def testMinimumSpanningTree(): sd = np.random.randint(1, 999) N = 150 makeCanvas(size=[7, 7]) G = randomNodes(N, .1, NodeColor='black', NodeSize=.05, seed=sd) complete(G) t0 = time() Krus = KruskalMinTree(G, modify=False) print("Kruskal: {:.3f} seconds".format(time() - t0)) #print(Krus) empty(G) G.addEdges(Krus) G.drawNodes() G.drawLines() makeCanvas(size=[7, 7]) G = randomNodes(N, .1, NodeColor='black', NodeSize=.05, seed=sd) complete(G) t0 = time() Prim = PrimMinTree(G, modify=False) print("Prim: {:.3f} seconds".format(time() - t0)) #print(Prim) empty(G) G.addEdges(Prim) G.drawNodes() G.drawLines() #testMinimumSpanningTree()
def testDijkstrasAlgorithmVisualizer(): #G = flowerSnarkGraph(NodeSize=.2) #DijkstraVisualizer(G,start=7,text=False) N = 15 G = randomNodes(N,1,NodeSize=.15,TextSize=1.7) G.Mat = connectedGraph(N,prob=.1) DijkstraVisualizer(G,start=0,text=True)
def unitDiskGraph(N, f, d, xlim=[-2.8, 2.8], ylim=[-2.8, 2.8], seed=None, **kwargs): G = randomNodes(N, f, xlim=xlim, ylim=ylim, seed=seed, **kwargs) for i in range(N): for j in range(i): if dist(G.pos[i], G.pos[j]) < d: G.addEdges(i, j) return G
def testStronglyConnected(): G = randomNodes(10, .5, NodeSize=.2, TextSize=1.5, seed=44345) G.addEdges([8, 3, 6, 4, 1, 1, 7, 9, 7, 2, 2, 9, 2, 4, 7], [3, 6, 4, 8, 4, 7, 5, 7, 2, 0, 6, 0, 9, 3, 4], directed=True) makeCanvas() G.drawNodes() G.drawText() G.drawArrows() S = stronglyConnected(G) color = ['pink', 'cornflowerblue', 'khaki', 'lightgreen', 'violet'] ctr = 0 for i in S: for j in i: G.colors[j] = color[ctr] ctr += 1 G.drawNodes()
from Graphs import * from DijkstrasAlgorithm import dijkstra from RandomPoints import randomNodes G = randomNodes(14, .5, NodeSize=.2, seed=44345) G.addEdges([8, 3, 6, 4, 1, 1, 7, 9, 7, 2, 2, 9, 2, 5, 10, 0, 11, 4, 12, 1], [3, 6, 4, 8, 4, 7, 5, 7, 2, 0, 6, 0, 9, 10, 0, 11, 3, 12, 8, 13], directed=False) makeCanvas() G.drawNodes() G.drawText() D = maskDist(G) M = np.zeros([G.size, G.size]) for n in range(10): x = dijkstra(D, n) x = x[1] used = [] for i in x: for j in range(len(i) - 1): if [i[j], i[j + 1]] not in used: used.append([i[j], i[j + 1]]) for x in used: M[x[0], x[1]] += 1 M[x[1], x[0]] += 1 print(M)
# Draw the connections if len(P) > 1: G.addEdges([O[-1]], [O[-2]]) if directed == False: G.addEdges([O[-2]], [O[-1]]) # Check if we've reached the place where we started if end == P[0]: P.append(cur) G.addEdges([O[0]], [O[-1]]) if directed == False: G.addEdges([O[-1]], [O[0]]) break return O makeCanvas(size=[12, 12]) #np.random.seed(17) G = randomNodes(20, .3) G.texts = [i for i in range(20)] G.tscales = [2] * 20 G.radii = [.06] * 20 G.drawNodes() c = convexHull(G, directed=True) print(c) G.drawLines()
from Graphs import * from RandomPoints import randomNodes G = randomNodes(10, .5, NodeSize=.2, TextSize=1.5, seed=44345) G.addEdges([8, 3, 4, 1, 1, 7, 9, 7, 2, 2, 9, 2, 4, 7, 8, 2], [3, 6, 8, 4, 7, 5, 7, 2, 0, 6, 0, 9, 3, 4, 6, 8], directed=True) def mergeNode(a, b, G): A = [i for i in range(len(G.texts)) if G.texts[i] == str(a)][0] B = [i for i in range(len(G.texts)) if G.texts[i] == str(b)][0] x = np.mean([G.pos[A][0], G.pos[B][0]]) y = np.mean([G.pos[A][1], G.pos[B][1]]) G.Mat[A, :] += G.Mat[B, :] G.Mat[:, A] += G.Mat[:, B] G.delNode(B) G.pos[A] = [x, y] def mergeNodes(L, G, name=None): L.sort() for i in range(len(L) - 1): mergeNode(L[0], L[i + 1], G) if name != None: G.texts[L[0]] = name