This repository was archived by the owner on Jan 11, 2018. It is now read-only.
forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
161 lines (136 loc) · 5.75 KB
/
Copy pathgraph.py
File metadata and controls
161 lines (136 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Author: OMKAR PATHAK
# Created On: 12th August 2017
class Graph(object):
def __init__(self):
self.graph = {}
self.count = 0
def print_graph(self):
''' for printing the contents of the graph '''
for i in self.graph:
print(i,'->',' -> '.join([str(j) for j in self.graph[i]]))
def add_edge(self, from_vertex, to_vertex):
''' function to add an edge in the graph '''
# check if vertex is already present
if from_vertex in self.graph.keys():
self.graph[from_vertex].append(to_vertex)
self.count += 1
else:
self.graph[from_vertex] = [to_vertex]
self.graph[to_vertex] = []
self.count += 1
def get_code(self):
''' returns the code for the current class '''
import inspect
return inspect.getsource(Graph)
class TopologicalSort(Graph):
def topological_sort(self):
''' function for sorting graph elements using topological sort '''
visited = [False] * self.count # Marking all vertices as not visited
stack = [] # Stack for storing the vertex
for vertex in range(self.count):
# Call the recursive function only if not visited
if visited[vertex] == False:
self.topological_sort_rec(vertex, visited, stack)
return stack
def topological_sort_rec(self, vertex, visited, stack):
''' Recursive function for topological Sort '''
# Mark the current node in visited
visited[vertex] = True
# mark all adjacent nodes of the current node
try:
for adjacent_node in self.graph[vertex]:
if visited[adjacent_node] == False:
self.topological_sort_rec(adjacent_node, visited, stack)
except KeyError:
return
# Push current vertex to stack which stores the result
stack.insert(0,vertex)
def get_code(self):
''' returns the code for the current class '''
import inspect
return inspect.getsource(TopologicalSort)
class CheckCycleDirectedGraph(object):
def __init__(self):
self.graph = {}
self.count = 0
def print_graph(self):
''' for printing the contents of the graph '''
for i in self.graph:
print(i,'->',' -> '.join([str(j) for j in self.graph[i]]))
def add_edge(self, from_vertex, to_vertex):
''' function to add an edge in the graph '''
# check if vertex is already present
if from_vertex in self.graph.keys():
self.graph[from_vertex].append(to_vertex)
self.count += 1
else:
self.graph[from_vertex] = [to_vertex]
self.count += 1
def check_cycle(self):
''' This function will return True if graph is cyclic else return False '''
visited = [False] * len(self.graph)
stack = [False] * len(self.graph)
for vertex in range(len(self.graph)):
if visited[vertex] == False:
if self.check_cycle_rec(visited, stack, vertex) == True:
return True
return False
def check_cycle_rec(self, visited, stack, vertex):
''' Recursive function for finding the cycle '''
# Mark the current node in visited and also add it to the stack
visited[vertex] = True
stack[vertex] = True
# mark all adjacent nodes of the current node
for adjacentNode in self.graph[vertex]:
if visited[adjacentNode] == False:
if self.check_cycle_rec(visited, stack, adjacentNode) == True:
return True
elif stack[adjacentNode] == True:
return True
# The node needs to be poped from
# recursion stack before function ends
stack[vertex] = False
return False
def get_code(self):
''' returns the code for the current class '''
import inspect
return inspect.getsource(CheckCycleDirected)
class CheckCycleUndirectedGraph(object):
def __init__(self):
self.graph = {}
self.count = 0
def print_graph(self):
''' for printing the contents of the graph '''
for i in self.graph:
print(i,'->',' -> '.join([str(j) for j in self.graph[i]]))
def add_edge(self, fromVertex, toVertex):
''' for adding the edge beween two vertices '''
# check if vertex is already present,
if fromVertex in self.graph.keys() and toVertex in self.graph.keys():
self.graph[fromVertex].append(toVertex)
self.graph[toVertex].append(fromVertex)
else:
# else make a new vertex
self.graph[fromVertex] = [toVertex]
self.graph[toVertex] = [fromVertex]
def check_cycle(self):
''' This function will return True if graph is cyclic else return False '''
visited = [False] * len(self.graph) # Marking all vertices as not visited
for vertex in range(len(self.graph)):
# Call the recursive function only if not visited
if visited[vertex] == False:
if self.check_cycle_rec(visited, -1, vertex) == True:
return True
return False
def check_cycle_rec(self, visited, parent, vertex):
''' Recursive function for finding the cycle '''
# Mark the current node in visited
visited[vertex] = True
# mark all adjacent nodes of the current node
for adjacentNode in self.graph[vertex]:
if visited[adjacentNode] == False:
if self.check_cycle_rec(visited, vertex, adjacentNode) == True:
return True
elif parent != adjacentNode:
return True
return False