예제 #1
0
def test2():
    g = AdjacencyListDigraph()

    for i in range(7):
        g.add_vertex(i)

    g.add_edge(0, 1)
    g.add_edge(0, 2)
    g.add_edge(0, 3)
    g.add_edge(1, 3)
    g.add_edge(1, 4)
    g.add_edge(2, 3)
    g.add_edge(2, 5)
    g.add_edge(3, 4)
    g.add_edge(3, 6)
    g.add_edge(4, 6)
    g.add_edge(5, 6)

    capacity_function = {
        (0, 1): 8,
        (0, 2): 7,
        (0, 3): 3,
        (1, 3): 6,
        (1, 4): 4,
        (2, 3): 3,
        (2, 5): 3,
        (3, 4): 5,
        (3, 6): 3,
        (4, 6): 9,
        (5, 6): 8,
    }

    flow = {
        (0, 1): 5,
        (0, 2): 3,
        (0, 3): 2,
        (1, 3): 3,
        (1, 4): 2,
        (2, 3): 0,
        (2, 5): 3,
        (3, 4): 2,
        (3, 6): 3,
        (4, 6): 4,
        (5, 6): 3,
    }

    n = FlowNetwork(g, capacity_function, 0, 6)

    max_flow = ford_fulkerson(n)
    [print(f"{key}: {value}") for key, value in max_flow.items()]
예제 #2
0
def test3():
    g = AdjacencyListDigraph()

    for i in range(8):
        g.add_vertex(i)

    g.add_edge(0, 1)
    g.add_edge(0, 3)
    g.add_edge(0, 5)
    g.add_edge(1, 2)
    g.add_edge(2, 4)
    g.add_edge(2, 7)
    g.add_edge(3, 1)
    g.add_edge(3, 2)
    g.add_edge(3, 5)
    g.add_edge(4, 3)
    g.add_edge(4, 7)
    g.add_edge(5, 6)
    g.add_edge(6, 4)
    g.add_edge(6, 7)

    capacity_function = {
        (0, 1): 2,
        (0, 3): 5,
        (0, 5): 7,
        (1, 2): 5,
        (2, 4): 1,
        (2, 7): 6,
        (3, 1): 2,
        (3, 2): 2,
        (3, 5): 2,
        (4, 3): 3,
        (4, 7): 1,
        (5, 6): 3,
        (6, 4): 2,
        (6, 7): 8,
    }

    n = FlowNetwork(g, capacity_function, 0, 7)

    max_flow = ford_fulkerson(n)
    [print(f"{key}: {value}") for key, value in max_flow.items()]
예제 #3
0
from algorithms.graphs.dag_shortest import *
from data_structures.AdjacencyListDigraph import *

g = AdjacencyListDigraph()

for i in range(1, 7):
    g.add_vertex(i)

g.add_edge(1, 2, weight=12)
g.add_edge(1, 3, weight=14)
g.add_edge(2, 4, weight=-3)
g.add_edge(2, 5, weight=10)
g.add_edge(3, 4, weight=6)
g.add_edge(3, 5, weight=4)
g.add_edge(4, 5, weight=5)
g.add_edge(4, 6, weight=4)
g.add_edge(5, 6, weight=-2)

distances = dag_shortest(g, 1)

print(g)
print(distances)
예제 #4
0
from data_structures.AdjacencyListDigraph import *

g = AdjacencyListDigraph()

g.add_vertex(1, "ele1")
g.add_vertex(2, "ele2")
g.add_vertex(3, "ele3")
g.add_vertex(4, "ele4")

g.add_edge(1, 2)
g.add_edge(2, 3)

g.set_edge_field(1, 2, weight=10)
g.set_edge_field(2, 3, label=10)
g.set_vertex_field(1, label="VISITED")
g.set_vertex_field(1, label="UNEXPLORED")

print(g)
예제 #5
0
from data_structures.AdjacencyListDigraph import *
from algorithms.graphs.allpairs_shortest import *
import numpy as np

g = AdjacencyListDigraph()

g.add_vertex("BWI")
g.add_vertex("JFK")
g.add_vertex("MIA")
g.add_vertex("ORD")
g.add_vertex("DFW")
g.add_vertex("LAX")

g.add_edge("BWI", "JFK", weight=10)
g.add_edge("BWI", "MIA", weight=20)
g.add_edge("JFK", "ORD", weight=-8)
g.add_edge("JFK", "DFW", weight=-10)
g.add_edge("MIA", "JFK", weight=-15)
g.add_edge("MIA", "DFW", weight=-25)
g.add_edge("MIA", "LAX", weight=30)
g.add_edge("ORD", "BWI", weight=20)
g.add_edge("ORD", "DFW", weight=-10)
g.add_edge("ORD", "LAX", weight=3)
g.add_edge("DFW", "LAX", weight=12)

distances = allpairs_shortest(g)
print(np.array(distances))
예제 #6
0
from data_structures.AdjacencyListDigraph import *
from data_structures.FlowNetwork import *

g = AdjacencyListDigraph()

for i in range(0, 7):
    g.add_vertex(i)

g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(0, 3)
g.add_edge(1, 3)
g.add_edge(1, 4)
g.add_edge(2, 3)
g.add_edge(2, 5)
g.add_edge(3, 4)
g.add_edge(3, 6)
g.add_edge(4, 6)
g.add_edge(5, 6)

capacity_function = {
    (0, 1): 7,
    (0, 2): 6,
    (0, 3): 5,
    (1, 3): 1,
    (1, 4): 2,
    (2, 3): 3,
    (2, 5): 9,
    (3, 4): 5,
    (3, 6): 3,
    (4, 6): 6,