def Depth_First_Search(GG):1# Name: Patrick Coen2#Description: Incomplete Code for SA403 Final. Examines whether or not a graph is connected, and returns a list of function times.3#Input: GG = Digraph4#Output: Graph_Connected: Integer value (1 = Connected, 2 = Not Connected)5# finish_time: vector of finish times for the SCC algorithm67# NOTES8# This code is modified from my submission for the Depth_First_Search algorithm. To note, my submission for the Depth_First_Search HW dependend on help I received from 2/C Shires. As a result, this submission has been assisted (indirecrtly) by 2/C Shires.9# "colors:" 0 = unvisited, 1 = currently visiting, 2 = have been visited10# "Graph_Connected:" 0 = initial value, 1 = connected, 2 = not connected1112vertNum = GG.order(); # vertNum is an integer = number of vertices in graph13adjMat = GG.adjacency_matrix(); # adjMat = adjacency matrix of input graph H14color = matrix(1,vertNum); # color is the vector holding assigned color values for vertices. To start, it is all zeroes.15startVert = 0; # chosen arbitrarily16run_steps = 0; # run_steps will be the value we add to finish_time; for now, it is 0.17Graph_Connected = 0;18finish_time = matrix(1,vertNum); # finish_time is the vector holding all of the assigned finish times - its size is number of vertices, initially all 01920def Depth_First_Search_Visit(v0):21color[0,v0] = 1; # We examine the start_vertex, and declared it "currently visiting", or 122for counter in xrange(0,vertNum): # Runs loop for every vertex in the graph23if(adjMat[v0,counter] == 1): # If counter_vertex (one we are examining) is adjacent to the initial vertex...24if(color[0,counter] == 0): # ...and has "color" 0,25Depth_FirstSearch_Visit(counter); # Loops back through26#end if unvisited27#end if adjacent28#end for all vertices29color[0,v0] = 2; #If there are no remaining vertices adjacent to c with "color" 0, give vertex c "color" 2.303132Depth_First_Search_Visit(startVert);33for c in xrange(0,vertNum):34if(color[0,c] != 2):35Graph_Connected = 2;36return (Graph_Connected, finish_times) # If not all of the vertices have been visited (i.e., color=2), then the graph returns "false"37Graph_Connected = 1;38return (Graph_Connected, finish_times) #Otherwise, function returns true39404142def Random_Digraph(n,p):43GG = digraphs.RandomDirectedGNP(n,p,loops=False)44return GG4546def Reverse_Digraph(G):47G_Reverse = G.reverse()48return G_Reverse4950def Order_List(list):51length = len(list);52ordered_list = [0]*length;53L = [i[0] for i in sorted(enumerate(list), key=lambda x:x[1])]54for i in range(0,length):55j = L[i]56ordered_list[i] = list[j]57return ordered_list5859def Reverse_list(list):60length = len(list);61for i in range(0,length)62j = length - i63reverse_list[j] = list[i]64return reverse_list65