Tarjan's strongly connected components algorithm | source code

Graph Theory Algorithms Graph Theory Algorithms
6 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

Hello, and welcome back. My name is William and today we will be looking over some source code for Tarzan's strongly connected components algorithm. Make sure you watch the video prior to this one explaining how the algorithm works. This video is dedicated only to source code. The source code I will be presenting during this video can be found@github.com slash William slash algorithms, there should be a link in the description. Here we are in the source code for Tarzan's algorithm to find strongly connected components.

You'll notice that this source code is written in the Java programming language. So to get started, let's have a look at the constructor for the class. And you'll notice that it takes a graph as an adjacency list as an argument. But before we get into the details of this actual algorithm, I want to show you how the algorithm actually works in practice, if you're going to executed. So if we look at the main method, you'll notice that here is how the algorithm is meant to be used. You set up the graph, and then you run the solver.

So to begin with, you declare a variable called n, which is the number of nodes that are going to be in the graph. Then you create the graph. So this initializes, the adjacency, list for n nodes, and we look at that create graph method up here. All the does is it initializes, the adjacency list, and then populates that with empty lists, so that we can be ready to add edges to our directed graph. So if you want to add an edge to the graph, and you would call this method, give it the graph, give it the directed edge, so from a node to another node, and then it will add that edge to the graph. So I believe this graph is the one from The slides the the very last graph, if I recall correctly.

So to actually find the strongly connected components, you create an object of the solver, you give it the graph, and then you run the solver on the graph. So, this is what actually finds the strongly connected components and this will return the the array of low link values, then what I do is I dump all of these inside a Multimap so we can know for each connected component, which are the nodes associated with that connected component. And then all I do is I print out which groups which nodes are part of. So you notice that I print that there are three connected components, and here are the nodes and what connected components they belong to. So that's how you use the algorithm Let's see what it's doing. So we already went over the constructor, which passed in a graph extracts the size of the graph, and caches the adjacency list.

As other instance variables, we have a boolean variable with tracks whether or not we have already solved the problem, then two variables to count the number of strongly connected components and assign an ID to each node, an array to track whether or not a node is on the stack, and then two integer arrays to track the ID of each node and the loading values of each node, and finally, a stack. So if we look at the SEC count method, it runs the solver if has not yet been solved and simply returns the number of strongly connected components. Get SCC s method also simply runs the solver if it has not yet been run, and returns the low link values array. Now let's look at the solver itself. So it returns if it's already been solved because we don't want to do more work than we need to.

Inside the solid method, I initialize all our arrays, I also filled the IDS array with the unvisited token. So we can know whether or not a node has been visited or not. Recall that the IDS array keeps track of the ID of a node, but it also keeps track of whether or not a node has been visited. So iterate through each node and if node AI is unvisited, then start a depth first search at node i finally, mark that we have solved the strongly connected components for this graph. Inside the depth first search method, it's almost exactly like The slides, so do the housekeeping stuff, which is like push the current node on the stack, mark the current node as being on the stack, give the the current node and ID and the loading value because the first time we're visiting it, then iterate over all the neighbors of this node.

Do a depth first search if the node we're going to is unvisited. On the callback, check if it's on the stack and minutes loading value with where we were just at. And back here after we've visited all the neighbors of the node, then we check if we're at the start of a strongly connected component. And if we are we want to pop off all the nodes associated with that strongly connected component which are on stack. So I start with first node and myself conditionals to pop until I return back to the start of that strongly connected component. And as I'm popping off nodes from the stack, I mark the node as no longer being on the stack.

And I also assign every node part of that strongly connected component to have the same ID as the node which started the strongly connected component. Just so that we know after the fact, which nodes belong to which strongly connected component, finally, increment the number of strongly connected components in case we are interested in that. And that's basically Trojans algorithm in a nutshell, guys, thanks for watching. Let me know if you have any questions. And like this video and subscribe for more mathematics and computer science videos. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.