Skip to content

added graph algorithm #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions Graph_Algorithms/src/Length_of_cycles/length_of_cycles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'''
If a node is visited again in DFS it means there is a cycle. To get the length of that cycle, we save the parent of current node at every stage of that DFS.

Once a visited node is detected again, from this node go its parent and from there to its parent and so on till we reach the first node again. We keep track of how many nodes we have traversed in this process and that will be the length of the cycle.
'''


class Graph:
def __init__(self, nnodes, nedges):
self.nnodes = nnodes
self.nedges = nedges
self.adj = []
for i in range(nnodes+1):
temp = []
for j in range(nnodes+1):
temp.append(0)
self.adj.append(temp)
self.cyclens = []
#cyclens will save the length of all cycles

def addEdge(self, a, b):
self.adj[a][b] = 1
self.adj[b][a] = 1


#function to perform dfs and simultaneously calculate lengths of cycles in the graph
def dfsHelper(self, v, visited, myval, parent, parents):


parents[v] = parent
#save the parent if this node.


visited[v] = True
#mark this node as detected

for i in range(1, len(self.adj[0])):

if(self.adj[v][i] == 1):
if(visited[i] == False):
self.dfsHelper(i, visited, myval+1, v, parents)

else:

if(i!=parent):
#cycle detected

cur = parent
count =1
#start traversing the parents from here till we reach the current node again
while(cur!=i):
cur = parents[cur]

count+=1
if(count>self.nedges):
break

#save count which is the length of the cycle.
if(count <self.nedges):
self.cyclens.append(count+1)

visited[i] = True



#wrapper function to call the above helper function.
def dfs(self, v):
visited = [0 for i in range(self.nnodes+1)]
self.cyclens = []
parents = [0]*(self.nnodes+1)
self.dfsHelper(v, visited, 1, 0, parents)
print(sum(self.cyclens))