Skip to content

Commit

Permalink
Added more functionality
Browse files Browse the repository at this point in the history
Added topological sort, cycle detection and a function to report the nodes participating in cycles in graph(for a use case I myself needed ).
  • Loading branch information
A Safari committed Dec 14, 2018
1 parent 889f8fb commit b3a1517
Showing 1 changed file with 222 additions and 0 deletions.
222 changes: 222 additions & 0 deletions graphs/Directed and Undirected (Weighted) Graph
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,133 @@ class DirectedGraph:
def out_degree(self, u):
return len(self.graph[u])

def topological_sort(self, s = -2):
stack = []
visited = []
if s == -2:
s = list(self.graph.keys())[0]
stack.append(s)
visited.append(s)
ss = s
sorted_nodes = []

while True:
# check if there is any non isolated nodes
if len(self.graph[s]) != 0:
ss = s
for __ in self.graph[s]:
if visited.count(__[1]) < 1:
stack.append(__[1])
visited.append(__[1])
ss =__[1]
break

# check if all the children are visited
if s == ss :
sorted_nodes.append(stack.pop())
if len(stack) != 0:
s = stack[len(stack) - 1]
else:
s = ss

# check if se have reached the starting point
if len(stack) == 0:
return sorted_nodes

def cycle_nodes(self):
stack = []
visited = []
s = list(self.graph.keys())[0]
stack.append(s)
visited.append(s)
parent = -2
indirect_parents = []
ss = s
anticipating_nodes = set()

while True:
# check if there is any non isolated nodes
if len(self.graph[s]) != 0:
ss = s
for __ in self.graph[s]:
if visited.count(__[1]) > 0 and __[1] != parent and indirect_parents.count(__[1]) > 0 and not on_the_way_back:
l = len(stack) - 1
while True and l >= 0:
if stack[l] == __[1]:
anticipating_nodes.add(__[1])
break
else:
anticipating_nodes.add(stack[l])
l -= 1
if visited.count(__[1]) < 1:
stack.append(__[1])
visited.append(__[1])
ss =__[1]
break

# check if all the children are visited
if s == ss :
stack.pop()
on_the_way_back = True
if len(stack) != 0:
s = stack[len(stack) - 1]
else:
on_the_way_back = False
indirect_parents.append(parent)
parent = s
s = ss

# check if se have reached the starting point
if len(stack) == 0:
return list(anticipating_nodes)

def has_cycle(self):
stack = []
visited = []
s = list(self.graph.keys())[0]
stack.append(s)
visited.append(s)
parent = -2
indirect_parents = []
ss = s
anticipating_nodes = set()

while True:
# check if there is any non isolated nodes
if len(self.graph[s]) != 0:
ss = s
for __ in self.graph[s]:
if visited.count(__[1]) > 0 and __[1] != parent and indirect_parents.count(__[1]) > 0 and not on_the_way_back:
l = len(stack) - 1
while True and l >= 0:
if stack[l] == __[1]:
anticipating_nodes.add(__[1])
break
else:
return True
anticipating_nodes.add(stack[l])
l -= 1
if visited.count(__[1]) < 1:
stack.append(__[1])
visited.append(__[1])
ss =__[1]
break

# check if all the children are visited
if s == ss :
stack.pop()
on_the_way_back = True
if len(stack) != 0:
s = stack[len(stack) - 1]
else:
on_the_way_back = False
indirect_parents.append(parent)
parent = s
s = ss

# check if se have reached the starting point
if len(stack) == 0:
return False

class Graph:
def __init__(self):
Expand Down Expand Up @@ -214,3 +341,98 @@ class Graph:
return visited
def degree(self, u):
return len(self.graph[u])

def cycle_nodes(self):
stack = []
visited = []
s = list(self.graph.keys())[0]
stack.append(s)
visited.append(s)
parent = -2
indirect_parents = []
ss = s
anticipating_nodes = set()

while True:
# check if there is any non isolated nodes
if len(self.graph[s]) != 0:
ss = s
for __ in self.graph[s]:
if visited.count(__[1]) > 0 and __[1] != parent and indirect_parents.count(__[1]) > 0 and not on_the_way_back:
l = len(stack) - 1
while True and l >= 0:
if stack[l] == __[1]:
anticipating_nodes.add(__[1])
break
else:
anticipating_nodes.add(stack[l])
l -= 1
if visited.count(__[1]) < 1:
stack.append(__[1])
visited.append(__[1])
ss =__[1]
break

# check if all the children are visited
if s == ss :
stack.pop()
on_the_way_back = True
if len(stack) != 0:
s = stack[len(stack) - 1]
else:
on_the_way_back = False
indirect_parents.append(parent)
parent = s
s = ss

# check if se have reached the starting point
if len(stack) == 0:
return list(anticipating_nodes)

def has_cycle(self):
stack = []
visited = []
s = list(self.graph.keys())[0]
stack.append(s)
visited.append(s)
parent = -2
indirect_parents = []
ss = s
anticipating_nodes = set()

while True:
# check if there is any non isolated nodes
if len(self.graph[s]) != 0:
ss = s
for __ in self.graph[s]:
if visited.count(__[1]) > 0 and __[1] != parent and indirect_parents.count(__[1]) > 0 and not on_the_way_back:
l = len(stack) - 1
while True and l >= 0:
if stack[l] == __[1]:
anticipating_nodes.add(__[1])
break
else:
return True
anticipating_nodes.add(stack[l])
l -= 1
if visited.count(__[1]) < 1:
stack.append(__[1])
visited.append(__[1])
ss =__[1]
break

# check if all the children are visited
if s == ss :
stack.pop()
on_the_way_back = True
if len(stack) != 0:
s = stack[len(stack) - 1]
else:
on_the_way_back = False
indirect_parents.append(parent)
parent = s
s = ss

# check if se have reached the starting point
if len(stack) == 0:
return False

0 comments on commit b3a1517

Please sign in to comment.