def as_AL(self): adjlist = AL.Graph(self.vertices, self.weighted, self.directed) for i in self.el: adjlist.insert_edge(i.source, i.dest, i.weight) return adjlist
def kruskal(G,trace=False): weight_heap = min_heap.MinHeap() S = dsf.DSF(len(G.al)) for v in range(len(G.al)): for edge in G.al[v]: if v<edge.dest: weight_heap.insert(min_heap.HeapRecord(edge.weight,[v,edge.dest,edge.weight])) mst = graph.Graph(len(G.al),weighted=True) count = 0 while count<len(G.al)-1 and len(weight_heap.heap)>0: next_edge = weight_heap.extractMin().data if S.union(next_edge[0],next_edge[1])==1: mst.insert_edge(next_edge[0],next_edge[1],next_edge[2]) count+=1 if trace: draw_over(G,mst,'Kruskal '+str(count)) print(next_edge,'added to mst') else: if trace: print(next_edge,'rejected') if count == len(G.al)-1: return mst print('Graph has no minimum spanning tree') return None
def prim(G,origin=0, trace=False): weight_heap = min_heap.MinHeap() connected = set([origin]) for edge in G.al[origin]: weight_heap.insert(min_heap.HeapRecord(edge.weight,[origin,edge.dest,edge.weight])) mst = graph.Graph(len(G.al),weighted=True) count = 0 while count<len(G.al)-1 and len(weight_heap.heap)>0: next_edge = weight_heap.extractMin().data if next_edge[0] not in connected: new_vertex = next_edge[0] elif next_edge[1] not in connected: new_vertex = next_edge[1] else: continue mst.insert_edge(next_edge[0],next_edge[1],next_edge[2]) connected.add(new_vertex) count+=1 if trace: draw_over(G,mst,'Prim '+str(count)) print(next_edge,'added to mst') for edge in G.al[new_vertex]: if edge.dest not in connected: weight_heap.insert(min_heap.HeapRecord(edge.weight,[new_vertex,edge.dest,edge.weight])) if count == len(G.al)-1: return mst print('Graph has no minimum spanning tree') return None
def buildGraphRepresentations(): # vars v = 16 weighted = False directed = False # build adjacency list AL = graphAL.Graph(v, weighted, directed) AL.insert_edge(0, 5) AL.insert_edge(2, 11) AL.insert_edge(2, 7) AL.insert_edge(4, 5) AL.insert_edge(4, 7) AL.insert_edge(4, 13) AL.insert_edge(8, 11) AL.insert_edge(8, 13) AL.insert_edge(10, 11) AL.insert_edge(10, 15) # build adjacency matrix, build edge list AM = AL.as_AM() EL = AL.as_EL() # return all three representations return AL, AM, EL
def backtrackHamiltonian(G): # #check if every vertex in V has in-degree 2. gEL = G.as_EL() for e in gEL.el: print(e.source, e.dest, e.weight) gBuild = graph.Graph(len(G.al), weighted=G.weighted, directed=G.directed) return backtracker(gEL.el,gBuild.as_EL())
def as_AL(self): g_al = graph_AL.Graph(vertices=self.vertices, weighted=self.weighted, directed=self.directed) for edge in self.el: g_al.insert_edge(edge.source, edge.dest, edge.weight) return g_al
def randomizedHamiltonian(E, max_trials): #converting the adjacency list to edge list edgeList = E.as_EL() for i in range(max_trials): #getting random edges from the edge list Eh = random.sample(edgeList.el, len(E.al)) adj_list = graph.Graph(len(E.al), weighted=E.weighted, directed=E.directed) #creating adjacency list from the random list of edges for i in range(len(Eh)): adj_list.insert_edge(Eh[i].source, Eh[i].dest) #checking if there is one connected component if connected_components(adj_list) == 1: #iterating through the list to check if at #any point a vertex in degrees is not 2 #making it not have a hamiltonian cycle for i in range(len(adj_list.al)): if in_degree(adj_list, i) != 2: return False #not a hamiltonian cycle return True #is a hamiltonian cycle
def isHamiltonian(V, Eh): """ Determine whether a graph g = (V,Eh) has the necessary requirements to have a Hamiltonian cycle from the subset of edges Eh. """ # Build the graph g = (V,Eh) that will be used to determine if Eh forms # a Hamiltonian cycle. g = graph.Graph(V) for m in range(len(Eh)): source = Eh[m][0] dest = Eh[m][1] g.insert_edge(source, dest) # Determine if the graph g = (V,Eh) has 1 connected component. If it # has 1 connected component move on to determine if every vertex V has # an in-degree of 2. if cc.connected_components(g) == 1: # Determine if the in-degree of every vertex V is 2. If every # vertex has an in-degree of 2, return True to indicate that the subset # of edges Eh forms a Hamiltonian cycle. in_degrees_set = set(g.in_degrees()) if len(in_degrees_set) == 1 and next(iter(in_degrees_set)) == 2: return True # If the graph g = (V,Eh) fails either of the two requirements to have a # Hamiltonian cycle, return False. return False
def as_AL(self): g = al.Graph(len(self.am), self.weighted, self.directed) for i in range(len(self.am)): for j in range(len(self.am)): if self.am[i, j] > -1: g.insert_edge(i, j, self.am[i, j]) return g
def custom_graph(size, num_edges): g = AL.Graph(size) for i in range(num_edges): g.insert_edge(random.randint(0, size - 1), random.randint(0, size - 1)) return g
def as_AL(self): temp = gAL.Graph(16, directed=False) for x in range(len(self.am)): for y in range(len(self.am[x])): if self.am[x][y] != -1: temp.insert_edge(x, y) return temp
def build_graphAL(): g_al = AL.Graph(4039) f = open('facebook_combined.txt', 'r') for line in f: # Iterates until there is no line left to read in the document v = line.split(' ') # Array of size 2 containing source and dest g_al.insert_edge(int(v[0]), int(v[1])) f.close() return g_al
def as_AL(self): g = graph.Graph(6) for i in self.am[self.source]: print(i) #g.insert_edge(self,source,dest,weight=1) return
def three_layer_graph(a, b, c): g = graph_AL.Graph(a + b + c, directed=True) for i in range(a): for j in range(a, a + b): g.insert_edge(i, j) for i in range(a, a + b): for j in range(a + b, a + b + c): g.insert_edge(i, j) return g
def as_AL(self): #constructor fot AL g = gal.Graph(self.vertices, weighted=self.weighted, directed=self.directed) for edge in self.el: #iterate through edges list g.insert_edge(edge.source, edge.dest, edge.weight) #insert corresponding values return g
def backtrackingHamiltonian(G): # turn graph into edge list edgeList = G.as_EL() for e in edgeList.el: print(e.source, e.dest, e.weight) g = graph.Graph(len(G.al), weighted=G.weighted, directed=G.directed) return backtracking(edgeList.el, g.as_EL())
def as_AL(self): adjlist = AL.Graph( len(self.am), self.weighted, self.directed) # makes a AL graph that is same size as AM graph for row in range(len(self.am)): for col in range(len(self.am[row])): if self.am[row][col] != -1: adjlist.insert_edge(row, col, self.am[row][col]) return adjlist
def as_AL(self): # create an empty graph with the same length as current graph adjlist = AL.Graph(self.vertices, self.weighted, self.directed) # insert edges using a loop for i in self.el: adjlist.insert_edge(i.source, i.dest, i.weight) return adjlist
def as_AL(self): adjlist = AL.Graph(len(self.am), self.weighted, self.directed) for row in range(len(self.am)): for col in range(len(self.am[row])): if self.am[row][col] != -1: adjlist.insert_edge(row, col, self.am[row][col]) return adjlist
def three_layer_graph(a, b, c): g = graph_AL.Graph(a + b + c, directed=True) for i in range(a + b + c): if i < a: for j in range(a, a + b): g.insert_edge(i, j) elif i >= a and i < b + c: for j in range(a + b, a + b + c): g.insert_edge(i, j) return g
def am_to_al(g_am): g_al = graphAL.Graph(g_am.am.shape[0], directed=g_am.directed, weighted=g_am.weighted) for v in range(g_am.am.shape[0]): for e in range(g_am.am.shape[1]): w = g_am.am[v][e] if w != -1 and v < e: g_al.insert_edge(v, e, w) return g_al
def as_AL(self): # create an empty graph with the same length as current graph adjlist=AL.Graph(len(self.am), self.weighted, self.directed) # insert edges using a nested loop for row in range(len(self.am)): for col in range(len(self.am[row])): if self.am[row][col]!=-1: adjlist.insert_edge(row,col,self.am[row][col]) return adjlist
def generate_graph(vertices): """ Generate and return a complete graph represented as an adjacency list, in which every vertex V is connected to every other vertex. """ g = graph.Graph(vertices) for source in range(vertices): for dest in range(vertices): if source != dest: g.insert_edge(source, dest) return g
def as_AL(self): #constructor for AL graph g = gal.Graph(len(self.am), weighted=self.weighted, directed=self.directed) for source in range(len(self.am)): #itereates length for dest in range(len(self.am)): #iterates width if (self.am[source, dest] != -1): #only values existing g.insert_edge(source, dest, self.am[source, dest]) #insert corresponding values return g
def pathToGraph(vertices, path): """ Traverse through a path found either by breadth-first search or by depth-first search and insert an edge between two adjacent vertices in the path into the graph. """ G = graph.Graph(len(vertices)) for i in range(len(path) - 1): source = path[i] dest = path[i + 1] G.insert_edge(source, dest) return G
def show_AL(): g = graph.Graph(6, weighted=True, directed=True) g.insert_edge(0, 1, 4) g.insert_edge(0, 2, 3) g.insert_edge(1, 2, 2) g.insert_edge(2, 3, 1) g.insert_edge(3, 4, 5) g.insert_edge(4, 1, 4) g.delete_edge(1, 2) g.display() g.draw() return g
def randomized_hamiltonian(V, test_range): edge_list = V.as_EL() for t in range(test_range): edge = random.sample(edge_list.el, len(V.al)) # random edge from EL # inserts random edges into AL al = AL.Graph(len(V.al), weighted=V.weighted, directed=V.directed) for i in range(len(edge)): al.insert_edge(edge[i].source, edge[i].dest) if connected_components(al) == 1: for i in range(len(al.al)): if in_degree(al, i) != 2: return False return True
def R_Hamiltonian(V, E): edge_list = V.as_EL() for i in range(E): Eh = random.sample(edge_list.el, len(V.al)) # Creates an adjecenct list with the generated edges al = AL.Graph(len(V.al), weighted = V.weighted, directed = V.directed) for j in range(len(Eh)): al.insert_edge(Eh[j].source, Eh[j].dest) if connected_components(al) == 1: for i in range(len(al.al)): if in_degree(al, i) != 2: return False return True
def as_AL(self): g = graph_AL.Graph(len(self.am), weighted=self.weighted, directed=self.directed) for i in range(len(self.am)): for j in range(len(self.am[i])): if self.am[i][j] != 0: if not self.directed: if i < j: # prevent double edges g.insert_edge(i, j, self.am[i][j]) else: g.insert_edge(i, j, self.am[i][j]) return g
def as_AL(self): g = graph_AL.Graph(self.vertices, weighted=self.weighted, directed=self.directed) for i in range(self.vertices): for j in self.el: if j.source == i: if not self.directed: if i < j.dest: # prevent double edges g.insert_edge(i, j.dest, j.weight) else: g.insert_edge(i, j.dest, j.weight) return g