Quick actions

cmd+k|ctrl+k

Navigation

Languages

MyGraph

Snippet info

Language

Python

Visibility

public

Author

arthurpc02

Created

2025-01-23T20:38:15.05049Z

Updated

2025-01-23T21:13:56.259512Z

"""
My implementation of a graph using an adjacent list

it should look like this:
{
    0:[1,2] 
    1:[0,2,3]
    2:[0,1,4]
    3:[1,4]
    4:[2,3,5]
    5:[4,6]
    6:[5]
}
"""


class Graph:
    def __init__(self):
        self.numberOfNodes = 0
        self.adjacentList = {}
        print("instantiated")
        
    def addVertex(self, node):
        self.adjacentList[node] = []
        self.numberOfNodes+=1
    
    def addEdge(self, node1, node2):
        self.adjacentList[node1].append(node2)
        self.adjacentList[node2].append(node1)
    
    
    def showConnections(self):
        print(self.adjacentList)
        pass
        




if __name__ == "__main__":
    print("Hello World!\n")
    myGraph = Graph()
    myGraph.addVertex('0')
    myGraph.addVertex('1')
    myGraph.addVertex('2')
    myGraph.addVertex('3')
    myGraph.addVertex('4')
    myGraph.addVertex('5')
    myGraph.addVertex('6')
    print(f"number of nodes: {myGraph.numberOfNodes}")
    assert(myGraph.numberOfNodes == 7)
    
    myGraph.addEdge('0','1')
    myGraph.addEdge('0','2')
    myGraph.addEdge('1','2')
    myGraph.addEdge('1','3')
    myGraph.addEdge('2','4')
    myGraph.addEdge('3','4')
    myGraph.addEdge('4','5')
    myGraph.addEdge('5','6')
    
    myGraph.showConnections()
    
    print("\nGood bye cruel World!")
    
    
INFO