def opgD(f, s, g, u, d):
    g = Graph()

    #opretter alle hjørner
    for i in range(1, f):
        g.insert_vertex(i)

    #indsætter kanter
    myVertices = list(g.vertices())
    for i in range(1, f, u):
        g.insert_edge(i, myVertices)
def build_graph(routes):

    G = Graph()

    for r in routes:  # r = [a, b, c], a & b: Vertices, c: int edge label.
        a1 = r[0]
        a2 = r[1]
        c = r[2]

        v1 = Vertex(a1)
        v2 = Vertex(a2)

        vertices = list(G.vertices())

        if vertices:
            found_v1 = False
            found_v2 = False
            v1_str = v1.__str__()
            v2_str = v2.__str__()

            for v in vertices:
                v_str = v.__str__()

                if v_str == v1_str:
                    found_v1 = True
                    va = v
                elif v_str == v2_str:
                    found_v2 = True
                    vb = v
                else:
                    continue
            if not found_v1:
                va = G.insert_vertex(a1)
            if not found_v2:
                vb = G.insert_vertex(a2)
        else:
            va = G.insert_vertex(a1)
            vb = G.insert_vertex(a2)

        G.insert_edge(c, va, vb)

    return G
    def _findResidualNetwork(self):
        graph = self._graph
        residualGraph = Graph()
        residualGraph._source = graph.source
        residualGraph._sink = graph.sink

        # laver en residual edge re med en eksra attribut cf, som indeholder
        # hvor meget mere flow denne edge kan tage
        for v in graph.vertices():
            residualGraph.insert_vertex(v)

        print("***Residualnetwork edges before***")
        for e in graph.edges():
            #re == residual edge
            re = Vertex(e.element())
            re.cf = e.capacity() - e.element()
            residualGraph.insert_edge(e.element(), e.capacity(), e.vertexFrom(), e.vertexTo())

            print("e.element: ", e.element(), "e.capacity: ", e.capacity())
            print("re.cf ", re.cf)


        return residualGraph
Ejemplo n.º 4
0
def RobotOnAGrid(num, maze):
    graph = Graph()
    graph.setEnd(str(len(maze) - 1) + " " + maze[len(maze) - 1])
    graph.setStart(str(0) + " " + maze[0])
    v1 = None
    v3 = None
    v2 = None
    dfs = dfs_iterator()

    #Laver knuder for alle "," tegene i mazeen
    for i in range(len(maze)):
        #Skifter linje
        if i % 4 == 0 and i != 0:
            pass
        #Tilføjer knuder til højre
        elif maze[i] != '#' and maze[i + 1] != '#':
            v1 = graph.insert_vertex(str(i) + " " + str(maze[i]))
            v2 = graph.insert_vertex(str(i) + " " + str(maze[i + 1]))

            graph.insert_edge("R", v1, v2)
        #Tilføj knuder ned af
        elif maze[i + 5] != '#' and maze[i + num] != None:
            v3 = graph.insert_vertex(str(i) + " " + str(maze[i + num]))

            graph.insert_edge("D", v1, v3)
        #tilføj knuder til venstre
        elif maze[i] != '#' and maze[i - 1] != '#' and maze[i - 1] != None:
            v1 = graph.insert_vertex(str(i) + " " + str(maze[i]))
            v2 = graph.insert_vertex(str(i) + " " + str(maze[i + 1]))
            graph.insert_edge("L", v2, v1)
        #tilføj knuder op af
        elif maze[i] != '#' and maze[i - num] != None:
            v3 = graph.insert_vertex(str(i) + " " + str(maze[i - num]))
            graph.insert_edge("U", v3, v1)

    return dfs.DFS(graph)
Ejemplo n.º 5
0
        biggest = A
        smallest = B
    else:
        biggest = B
        smallest = A

    for i in range(len(smallest)):
        biggest.append(smallest[i])
        smallest.pop(i)


g = Graph()

v15 = g.insert_vertex(15)
v6 = g.insert_vertex(6)
v38 = g.insert_vertex(38)
v123 = g.insert_vertex(123)
v66 = g.insert_vertex(66)

g.insert_edge(10, v15, v38)
g.insert_edge(23, v15, v6)
g.insert_edge(90, v15, v66)
g.insert_edge(8, v66, v6)
g.insert_edge(2, v66, v38)
g.insert_edge(76, v66, v123)
g.insert_edge(7, v123, v6)
g.insert_edge(55, v123, v38)

for e in kruskal(g):
    print(str(e))
Ejemplo n.º 6
0
from edge_list_graph import Edge as Edge
from edge_list_graph import Graph as Graph
from edge_list_graph import Vertex as Vertex

from adjacency_list_graph import Edge as Edge
from adjacency_list_graph import Graph as Graph
from adjacency_list_graph import Vertex as Vertex
g = Graph()

g.insert_vertex(15)
g.insert_vertex(6)
g.insert_vertex(38)
g.insert_vertex(123)
g.insert_vertex(66)

g.insert_edge(10,15,38)
g.insert_edge(23, 15,6)
g.insert_edge(90, 15,66)
g.insert_edge(8, 66,6)
g.insert_edge(2, 66,38)
g.insert_edge(76, 66,123)
g.insert_edge(7, 123,6)
g.insert_edge(55, 123,38)


LastVisitet = 0
mx = -1
mySet = set()
def max_value(graf : Graph, vertex):
    global mx
                    if w == graph.sink:
                        return
                    if self._unexploredV.__contains__(w):
                        self._DFS(w)
g = Graph()

v15 = g.insert_vertex(15)
v6 = g.insert_vertex(6)
v38 = g.insert_vertex(38)
v123 = g.insert_vertex(123)
v66 = g.insert_vertex(66)

g.source = v15
g.sink = v123

g.insert_edge(10,20,v15,v38)
g.insert_edge(23, 30, v15,v6)
g.insert_edge(90,100, v15,v66)
g.insert_edge(8, 16,v66,v6)
g.insert_edge(2, 16, v66,v38)
g.insert_edge(76, 100,v66,v123)
g.insert_edge(7, 24,v123,v6)
g.insert_edge(55,70, v123,v38)

ff = ford_fulkerson(g)





for e in ff.DFS(ff._findResidualNetwork()).edges():