Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to traverse all edges in a graph

As a personal easter project I'm trying to implement some model-based testing at work. I've got a graph implemented in python and I need to traverse all edges / do all transitions of the graph, at least once. Traversing an edge twice or more does not matter, but I need to start and end in the same node and get a sequence of edges/transitions back.

Simpler algorithm > shortest sequence.

I've looked around and found a lot of algorithms, but I couldn't find one / a combination that works for me. It would be great if someone could point me in the right direction or give me some tips on how to do this.

My graph implementation looks like this:

graph = {
    A : {'IDLE': 't8', 'B': 't2', 'C': 't4'},
    B : {'A': 't3', 'C': 't4', 'IDLE': 't6'},
    C : {'A': 't5', 'IDLE': 't7', 'B': 't2'},
    IDLE : {'A': 't1'}
}

graph

like image 498
kristus Avatar asked Apr 06 '12 11:04

kristus


People also ask

Does DFS go through all edges?

As you can see from the example, DFS doesn't go through all edges. The vertices and edges, which depth-first search has visited is a tree. This tree contains all vertices of the graph (if it is connected) and is called graph spanning tree. This tree exactly corresponds to the recursive calls of DFS.

Does BFS traverse have every edge?

In BFS, all nodes are going to be visited. The minimum spanning tree contains a minimal number of edges to connect all the nodes. So, the BFS traversal will traverse all the edges in order to visit all the nodes.

What is edges in algorithm?

An edge (or link) of a network (or graph) is one of the connections between the nodes (or vertices) of the network. Edges can be directed, meaning they point from one node to the next, as illustrated by the arrows in the first figure below.

How does DFS algorithm work?

Depth-first search (DFS) is an algorithm for searching a graph or tree data structure. The algorithm starts at the root (top) node of a tree and goes as far as it can down a given branch (path), then backtracks until it finds an unexplored path, and then explores it.


1 Answers

Approach 1

You can change your graph G into a new graph G' where each edge in G becomes a node in G'. Make an edge from t1 to t2 in G' if "A -> t1 -> B -> t2 -> C" is possible in G for some A,B and C.

Then you need to find a path cover in G'.

Approach 2

  • Your position P initially is some node P0 (e.g. idle).
  • For each edge T (from A to B), find any route from P to A, then use T to go to B. Update P to be B.
  • Finally find any route from P back to P0.
like image 172
Mark Byers Avatar answered Oct 12 '22 02:10

Mark Byers