File tree Expand file tree Collapse file tree 3 files changed +162
-0
lines changed
Expand file tree Collapse file tree 3 files changed +162
-0
lines changed Original file line number Diff line number Diff line change 1+ # Author: OMKAR PATHAK
2+
3+ # We can use Python's dictionary for constructing the graph
4+
5+ class AdjacencyList (object ):
6+ def __init__ (self ):
7+ self .List = {}
8+
9+ def addEdge (self , fromVertex , toVertex ):
10+ # check if vertex is already present
11+ if fromVertex in self .List .keys ():
12+ self .List [fromVertex ].append (toVertex )
13+ else :
14+ self .List [fromVertex ] = [toVertex ]
15+
16+ def printList (self ):
17+ for i in self .List :
18+ print (i ,'->' ,' -> ' .join ([str (j ) for j in self .List [i ]]))
19+
20+ if __name__ == '__main__' :
21+ al = AdjacencyList ()
22+ al .addEdge (0 , 1 )
23+ al .addEdge (0 , 4 )
24+ al .addEdge (4 , 1 )
25+ al .addEdge (4 , 3 )
26+ al .addEdge (1 , 0 )
27+ al .addEdge (1 , 4 )
28+ al .addEdge (1 , 3 )
29+ al .addEdge (1 , 2 )
30+ al .addEdge (2 , 3 )
31+ al .addEdge (3 , 4 )
32+
33+ al .printList ()
34+
35+ # OUTPUT:
36+ # 0 -> 1 -> 4
37+ # 1 -> 0 -> 4 -> 3 -> 2
38+ # 2 -> 3
39+ # 3 -> 4
40+ # 4 -> 1 -> 3
Original file line number Diff line number Diff line change 1+ # Author: OMKAR PATHAK
2+
3+ class Graph ():
4+ def __init__ (self ):
5+ self .vertex = {}
6+
7+ # for printing the Graph vertexes
8+ def printGraph (self ):
9+ for i in self .vertex .keys ():
10+ print (i ,' -> ' , ' -> ' .join ([str (j ) for j in self .vertex [i ]]))
11+
12+ # for adding the edge beween two vertexes
13+ def addEdge (self , fromVertex , toVertex ):
14+ # check if vertex is already present,
15+ if fromVertex in self .vertex .keys ():
16+ self .vertex [fromVertex ].append (toVertex )
17+ else :
18+ # else make a new vertex
19+ self .vertex [fromVertex ] = [toVertex ]
20+
21+ def BFS (self , startVertex ):
22+ # Take a list for stoting already visited vertexes
23+ visited = [False ] * len (self .vertex )
24+
25+ # create a list to store all the vertexes for BFS
26+ queue = []
27+
28+ # mark the source node as visited and enqueue it
29+ visited [startVertex ] = True
30+ queue .append (startVertex )
31+
32+ while queue :
33+ startVertex = queue .pop (0 )
34+ print (startVertex , end = ' ' )
35+
36+ # mark all adjacent nodes as visited and print them
37+ for i in self .vertex [startVertex ]:
38+ if visited [i ] == False :
39+ queue .append (i )
40+ visited [i ] = True
41+
42+ if __name__ == '__main__' :
43+ g = Graph ()
44+ g .addEdge (0 , 1 )
45+ g .addEdge (0 , 2 )
46+ g .addEdge (1 , 2 )
47+ g .addEdge (2 , 0 )
48+ g .addEdge (2 , 3 )
49+ g .addEdge (3 , 3 )
50+
51+ g .printGraph ()
52+ print ('BFS:' )
53+ g .BFS (2 )
54+
55+ # OUTPUT:
56+ # 0 -> 1 -> 2
57+ # 1 -> 2
58+ # 2 -> 0 -> 3
59+ # 3 -> 3
60+ # BFS:
61+ # 2 0 3 1
Original file line number Diff line number Diff line change 1+ # Author: OMKAR PATHAK
2+
3+ class Graph ():
4+ def __init__ (self ):
5+ self .vertex = {}
6+
7+ # for printing the Graph vertexes
8+ def printGraph (self ):
9+ print (self .vertex )
10+ for i in self .vertex .keys ():
11+ print (i ,' -> ' , ' -> ' .join ([str (j ) for j in self .vertex [i ]]))
12+
13+ # for adding the edge beween two vertexes
14+ def addEdge (self , fromVertex , toVertex ):
15+ # check if vertex is already present,
16+ if fromVertex in self .vertex .keys ():
17+ self .vertex [fromVertex ].append (toVertex )
18+ else :
19+ # else make a new vertex
20+ self .vertex [fromVertex ] = [toVertex ]
21+
22+ def DFS (self ):
23+ # visited array for storing already visited nodes
24+ visited = [False ] * len (self .vertex )
25+
26+ # call the recursive helper function
27+ for i in range (len (self .vertex )):
28+ if visited [i ] == False :
29+ self .DFSRec (i , visited )
30+
31+ def DFSRec (self , startVertex , visited ):
32+ # mark start vertex as visited
33+ visited [startVertex ] = True
34+
35+ print (startVertex , end = ' ' )
36+
37+ # Recur for all the vertexes that are adjacent to this node
38+ for i in self .vertex .keys ():
39+ if visited [i ] == False :
40+ self .DFSRec (i , visited )
41+
42+ if __name__ == '__main__' :
43+ g = Graph ()
44+ g .addEdge (0 , 1 )
45+ g .addEdge (0 , 2 )
46+ g .addEdge (1 , 2 )
47+ g .addEdge (2 , 0 )
48+ g .addEdge (2 , 3 )
49+ g .addEdge (3 , 3 )
50+
51+ g .printGraph ()
52+ print ('DFS:' )
53+ g .DFS ()
54+
55+ # OUTPUT:
56+ # 0 -> 1 -> 2
57+ # 1 -> 2
58+ # 2 -> 0 -> 3
59+ # 3 -> 3
60+ # DFS:
61+ # 0 1 2 3
You can’t perform that action at this time.
0 commit comments