Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9278821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:28:25+00:00 2026-06-18T17:28:25+00:00

I have a simple graph and want to create a method get_edge that will

  • 0

I have a simple graph and want to create a method “get_edge” that will take two vertices as arguments and return an edge between them if it exists, and None otherwise. Here’s a snippet of what I’ve tried. It doesn’t work because it currently creates an object instead of checking if there is one in existence already. What’s the simplest way to write get_edge()?

def add_edge(self, e):
    """Adds and edge to the graph by adding an entry in both directions.

    If there is already an edge connecting these Vertices, the
    new edge replaces it.
    """
    v, w = e
    self[v][w] = e
    self[w][v] = e

def get_edge(self, v1, v2):
    try:
        Edge(v1, v2)
        print 'Edge exists'
    except:
        print 'Edge does not exist'
        return None
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:28:26+00:00Added an answer on June 18, 2026 at 5:28 pm

    I suspect you want something like:

    def get_edge(self, v1, v2):
        try:
            e = self[v1][v2] # order shouldn't matter
            print("edge exists")
            return e
        except KeyError:
            print("edge does not exist")
            return None
    

    I’m assuming your class is derived from dict or has a __getitem__ method that works and will raise a KeyError if you ask for a key that doesn’t exist. If you don’t need the print statements (that is, they’re just for debugging), you can do away with the e variable and just return the result directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a simple graph which will get the values and shows
Let's say that I want to create a simple graph representation where Vertex and
I have a very simple line graph I'm trying to create in gRaphael. My
I have a simple nested dict to indicate a graph with nodes, vertices, and
I have a simple graph with x and y axes. I don't want the
I want to create a graph using Jgraph, and later use that graph to
I have a simple weighted graph A 1 / \\ 0.5 / \\0.5 B
I have simple table in Sybase -- Creating table 'SimpleText' CREATE TABLE [dbo].[SimpleText] (
I have simple php validation form that is halfway working. If you leave the
I have simple win service, that executes few tasks periodically. How should I pass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.