Skip to content

Instantly share code, notes, and snippets.

@bvsbrk
Created September 16, 2017 05:10
Show Gist options
  • Save bvsbrk/27385946824723b8e48c9c75e48fc7ef to your computer and use it in GitHub Desktop.
Save bvsbrk/27385946824723b8e48c9c75e48fc7ef to your computer and use it in GitHub Desktop.
Description
class Vertex:
def __init__(self, id):
self.connections = {}
self.id = id
def addNeighbor(self, id, weight):
if id not in self.connections:
@bvsbrk
Copy link
Author

bvsbrk commented Sep 16, 2017

-- > Vertex is a node in Graph

Each vertex will have an id and a dictionary (actually it is adjacency list).

The dictionary is implemented as follows :

The key in the dictionary is the node which is connected to this vertex and its value is the weight of the edge that connects this vertex to this node

Eg : a is connected to b and the weight of edge is 1 and a is connected to c and edge weight is 2

The adjacency list looks like this

a ==> {
"b" : 1,
"c" : 2
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment