Backend side of SCC and Bridges Finder using Tarjans Algorithm with React Typescript and Golang
This program is created to find s Strongly Connected Component (SCC) on a directed Graph and Bridges on a undirected graph based on user input. The program will proceed a.txt file which containt directed adjacency lists to interpret a graph, then find the SCC and Bridges using popular SCC algorithm, Tarjans. User can also modify the graph by adding and deleting edges by filling the form. The program also provide a good graph and edge coloring to make a better undersanding about the results. Furthermore, the project information is also provided for future improvements.
.
โโโโ algorithm
โ โโโโ algorithm.go
โโโโ middleware
โ โโโโ handlers.go
โโโโ router
โ โโโโ router.go
โโโโ go.mod
โโโโ go.sum
โโโโ main.go
โโโโ README.md
- gorilla/mux (v 1.8.0) to handle routing
- time to hanlde processing time
- io/ioutil to unmarshal request body
- net/http to handle request and responseWriter
- encoding/json to parse the json POST body
Thus section will explain what is Tarjans Algorithm and how it used to detect Strongly Connected Components (SCC) on directed graph and bridges on undirected graph.
Tarjan's algorithm is a graph traversal algorithm developed by Robert Tarjan in 1972. It is used to find strongly connected components (SCCs) in a directed graph. SCCs are subgraphs in which there is a path between any two vertices.
Tarjan's algorithm is based on depth-first search (DFS) and uses a concept called the "low-link value" to determine SCCs. The algorithm assigns a unique identifier called a "DFS number" to each vertex during the DFS traversal. It also maintains a stack of vertices that are currently being explored.
- Start with an unvisited vertex and initiate a DFS traversal from that vertex.
- Assign a DFS number to the current vertex and set its low-link value to the DFS number.
- Push the current vertex onto the stack.
- Explore all the neighbors of the current vertex. If a neighbor has not been visited, recursively traverse it using DFS and update the low-link value of the current vertex if necessary.
- After exploring all the neighbors, if the low-link value of the current vertex is equal to its DFS number, then it is the root of an SCC. Pop vertices from the stack until the current vertex is reached, and add the popped vertices to the SCC.
- Repeat steps 1-5 for any unvisited vertices until all vertices have been visited.
At the end of the algorithm, the algorithm outputs the SCCs found in the graph.
Tarjan's algorithm has a same time complexity as DFS,
Consider there are graph as following
graph LR
1((1)) --> 2((2))
1((1)) --> 3((3))
1((1)) --> 8((8))
2((2)) --> 4((4))
3((3)) --> 5((5))
4((4)) --> 6((6))
5((5)) --> 4((4))
5((5)) --> 7((7))
5((5)) --> 8((8))
6((6)) --> 2((2))
And doing DFS process from edge 1, then
- Tree Edge is an edge which is present in the tree obtained after applying DFS on the graph. In the example above, the tree edge is 1->2, 2->4, 4->6, 1->3, 3->5, 5->7, 5->8.
-
Forward Edge is an edge
$(u, v)$ such that$v$ is a descendant but not part of the DFS tree. In the example above, 1->8 is a forward edge. -
Back edge is an edge
$(u, v)$ such that$v$ is the ancestor of node$u$ but is not part of the DFS tree. In the example above, 6->2 is a back edge. Presence of back edge indicates a cycle in directed graph. - Cross Edge is an edge that connects two nodes such that they do not have any ancestor and a descendant relationship between them. In the example above, 5->4 is a cross edge.
Tarjans Algorithm should be simply modified to find all bridges in the graph. Bridges, also known as cut edges, are the edges in a graph whose removal increases the number of connected components. In other words, if a bridge edge is removed, the graph will become more disconnected.
To find bridges in a graph, Tarjan's algorithm could be extended by adding an additional step during the DFS traversal. Here is the detail
- Iterate over each neighbor
$v$ of vertex$u$ : - If
$v$ is not visited,- recursively visit it
- Update
lowLinks[u]
with the minimum oflowLinks[u]
andlowLinks[v]
. - If
lowLinks[v]
is greater thanindices[u]
orlowLinks[u]
is greater thanindices[v]
(must check both due to undirected graph), it means that the edge$(u, v)$ is a bridge. Add it to the bridges list.
- Else if
$v$ is visited and$v$ is not the parent of$u$ then updatelowLinks[u]
with the minimum oflowLinks[u]
anddisc[v]
.
Clone this repository from terminal with this command
$ git clone https://github.com/mikeleo03/Tarjans-Algorithm-Simulation_Backend.git
Compile by running the following command
$ go run main.go
If you do it correctly, the pogram should be running on localhost:8080.
https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm https://www.baeldung.com/cs/scc-tarjans-algorithm https://www.thealgorists.com/Algo/GraphTheory/Tarjan/SCC