Exemple #1
0
def test_cyclic():
    g = Graph()
    g.add_edge(Edge(1, 3))
    g.add_edge(Edge(2, 1))
    assert not g.cyclic()
    g.add_edge(Edge(3, 2))
    assert g.cyclic()
Exemple #2
0
def test_bellmanford():
    g = Graph()
    g.add_edge(Edge("a", "x", 1))
    g.add_edge(Edge("x", "y", -1))
    g.add_edge(Edge("y", "z", -1))
    g.add_edge(Edge("z", "x", -1))
    d, p = g.bellmanford("a")
    assert d["a"] == 0 and p["a"] is None
    assert d["x"] == -np.inf and p["x"] == "z"
    assert d["y"] == -np.inf and p["y"] == "x"
    assert d["z"] == -np.inf and p["z"] == "y"
Exemple #3
0
    def __init__(self, graph: gr.Graph, goal, h: Callable):
        """ Initializes the solver instance. Shortest path is found
            from first vertex of graph to the goal. 

            Parameters:
            graph: graph instance to be search
            goal: goal we are finding the shortest path to
            h: heuristic function used to search the path in graph
               Input value of function is vertex, return value is cost 
               as a number.

            Returns:
            None
        """
        first_vertex = graph.get_first_vertex()
        self._graph: gr.Graph = graph
        self._goal = goal
        self._h_func: Callable = h

        # already discovered nodes. Initialized with first vertex.
        # Each vertex is stored in pririty queue as tuple (priority, vertex)
        self._open_set = dpq.MinDictPQ()
        self._open_set.insert((0, first_vertex))

        # List of nodes already discovered and explored.
        # Starts off empty
        # Once a node has been 'current' it then goes here
        self._close_set = []

        # for node n, cameFrom[n] is the node immediately preceding it
        # on the cheapest path from start to n currently known.
        # Node is supposed to be grapth vetrex
        self._came_from = {}

        # g_score[n] is cost of the cheapest path from start to node n
        # known so far. Initially set to 0 for first vertex.
        self._g_score = {first_vertex: 0}
        self._f_score = {first_vertex: 0}
Exemple #4
0
def test_sssp():
    # sssp = single source shortest path
    # CLRS(2009) p659
    g = Graph()
    g.add_edge(Edge("s", "t", 10))
    g.add_edge(Edge("s", "y", 5))
    g.add_edge(Edge("t", "y", 2))
    g.add_edge(Edge("t", "x", 1))
    g.add_edge(Edge("x", "z", 4))
    g.add_edge(Edge("z", "x", 6))
    g.add_edge(Edge("z", "s", 7))
    g.add_edge(Edge("y", "t", 3))
    g.add_edge(Edge("y", "x", 9))
    g.add_edge(Edge("y", "z", 2))
    d, p = g.dijkstra("s")
    assert d["s"] == 0 and p["s"] is None
    assert d["t"] == 8 and p["t"] == "y"
    assert d["x"] == 9 and p["x"] == "t"
    assert d["z"] == 7 and p["z"] == "y"
    assert d["y"] == 5 and p["y"] == "s"
    d, p = g.bellmanford("s")
    assert d["s"] == 0 and p["s"] is None
    assert d["t"] == 8 and p["t"] == "y"
    assert d["x"] == 9 and p["x"] == "t"
    assert d["z"] == 7 and p["z"] == "y"
    assert d["y"] == 5 and p["y"] == "s"
Exemple #5
0
def test_topological():
    g = Graph()
    g.add_edge(Edge(1, 3))
    g.add_edge(Edge(2, 1))
    assert g.topological() == [2, 1, 3]
Exemple #6
0
def test_traverse():
    g = Graph()
    g.add_edge(Edge(1, 2))
    g.add_edge(Edge(1, 3))
    g.add_edge(Edge(2, 4))
    g.add_edge(Edge(2, 5))
    g.add_edge(Edge(5, 3))
    g.add_vertex(6)
    """ Test bfs """
    trace = []
    g.bfs(1, func_in=lambda vertex: trace.append(vertex))
    assert trace == [1, 2, 3, 4, 5]
    """ Test dfs """
    trace = []
    g.dfs(1, func_in=lambda vertex: trace.append(vertex))
    assert trace == [1, 2, 4, 5, 3]
    """ Test traverse """
    trace = []
    g.traverse(order=g.ORDER_BFS, func_in=lambda vertex: trace.append(vertex))
    assert trace == [1, 2, 3, 4, 5, 6]
    """ Test cyclic """
    g.add_edge(Edge(3, 2))
    assert g.cyclic()
    """ Test acyclic """
    g.remove_edge(Edge(3, 2))
    assert not g.cyclic()
    """ Test topological sort """
    assert g.topological() == [6, 1, 2, 5, 3, 4]
    """ Test sptree """
    assert not g.sptree().cyclic()