Dijkstra's algorithm is a single-source shortest path algorithm for weighted graphs with non-negative weights. It maintains three data structures: a distance array (initialized to infinity except the source at 0), a predecessor array (initialized to -1), and a set of unprocessed vertices. The algorithm repeatedly selects the unprocessed vertex with the minimum distance, applies edge relaxation to its neighbors (updating distances if a shorter path is found), and marks it as processed. The time complexity is O(E + V log V) when using a priority queue, or O(V² + E) with a simple array. The algorithm can be applied to both directed and undirected graphs, but requires non-negative edge weights to function correctly.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
COMP2521 26T2 Week 7 Lecture 2
Added:Hello everyone.
Okay.
Wow.
Hello everyone. Can you hear me?
Any question from last session?
Okay. Why is not Okay, no question.
So we have two more topics uh to discuss today about graph and then they will finish.
We will have two more session.
one is about trees again and another hash table. So uh if you remember last week we last session we discussed three different topics regarding directed graph finding um traversal finding cycle and also transitive closure and today we talking about dystra algorithm which is about finding the shortest path between vertics and also minimum spanning tree that we will discuss today also.
So let's see what is this Dextra algorithm.
Um if you remember in directed graph each edge has a direction. So we have a source and destination.
But here we want to uh actually discuss weighted directed graph. So we have a um edge between two nodes and also each one has a weight. This weight based on the problem can be distance can be cost or whatever.
Regardless what is this weight? We want to see how we can find shortest path between two nodes. But here because we have a weight the shortest path here the path has a shortest uh cost or distance between two nodes. We know that in undirected graph when we talking about shortest path it means the minimum number of edges between two nodes. But here is not about the minimum number of edges it's about minimum total cost or distance between between two uh vertics.
So we are trying finding the path between two vertics with a uh minimum cost.
So overall when we are talking about finding a shortest path between two vertics we can have three different type of problems. First is source and target.
So we have one source in a graph, a source vertx, a target vertx, and we want to just find the shortest path between this single source and single target.
Another category is uh single source. It means we have a single source but we want to find the shortest path between this single source and any other vertex in the graph.
And the last is all pairs. So we want to find the shortest path between any pair of the vertex dystra algorithm is with for the second one. So we are discussing about how we find the shortest path between a single source. So we choose one vertex and then we want to find what is the shortest distance or shortest path between this vertex versus all other vertics in the graph.
So in weighted graph this is important that sometimes uh using multiple ages to find the path uh has a total um less cost compared to use a direct edge between two vertics.
For example here we have a vertex zero and vertex one. If we choose the direct path between zero and one the cost is five. But if we go from 0 to two and then two to one the cost is four. So here actually the number of edges is not matter between source as destination the cost or distance is matter that which one has the shortest cost or distance.
But as I said in unirected graph the definition is different because there the number of edges between source and destination is important.
Any question so far?
So the algorithm that we are discussing the name is Dystra. That's the name of the person a Dutch computer scientist who uh introduced this algorithm.
uh so as I said in dyra algorithm we're trying to find the shortest path in a weighted graph with non negative weights. So this condition is very important because if you have a graph that has a negative weight then diccastra is not working. We we need to use some other algorithm and dicostra algorithm can work on directed and undirected doesn't matter but it should be weighted and weights should not be negative.
Uh here a summary of how this algorithm actually works and then we will see the example it will be more clear that what how actually we are finding the shortest path uh between two nodes.
Uh the first important point is as I mentioned in Dextra algorithm you are finding shortest path between one source uh and all other vertex. So it is a single source problem uh when you're using Dcastra.
So we start from source and gradually expand uh to find the closest reachable vertex.
Each time when we start dcastra algorithm we pick one vertx and do all the process on that vertx you will see and then we keep it side and go to the next one. So every stage we actually find the shortest paths uh against one single node and then we proceed to the next vertex. So in each stage we select one of the vertex from the unprocessed one that you will see what does it mean the one that has the shortest known distance so far and then we find u apply the dichestra algorithm on that single vertex and then go for the next one.
uh and each time when we choose a vertx we will check that if we use this vertx can we find a shorter uh path or not and if we find a shorter path we have few data structure that you will see we have to update that data structure first data structure about saving the distance we need to update this one and also uh we need to update the predecessor data structure Here also overall we have three data structure in Dextra. One is distance array will keep the shortest distance that we already find between our source and other vertx and it can be updated during the algorithm and at the end we find the shortest distance between source and any other vertx. Another data structure that we are using is predecessor is the same thing that we using for BFS DFS. It u actually keep track of the parent or the node that we uh from that node will reach to the current vertex.
And the third data structure is set of vertx and it shows that which vertx is already processed which one is not processed and every time we select one of them from this set and then apply diccastro algorithm on that. So overall we have a three data structures that we use. You will see how we use this tree.
So overall uh first we need to create and initialize all these three data structures.
Uh first we create distance array and initialize it with in infinity all of them.
And then we create predecessor array and initialize it to minus one. And also we initialize a set of vertx uh to list of the vertics that we have in the graph.
And every time we choose one and process that particular vertx and also at the start point we set the distance of the source to zero because when I am at source my distance to source is zero. So always the distance to source is zero and as long as still there is a vertx that is not processed yet we have to remove one of them from set of vertics the one that has a smallest distance and then apply edge relaxation on it that this is the main operation of dicestra that we'll discuss what does it mean edge relaxation this is overall how dichestra algorithm work we will see an example So as I mentioned this is the most important operation of dicestra edge relaxation. So let's see what does it mean.
Uh suppose uh this is a source node and this is one of the nodes that we want to find the shortest path between S and W.
And suppose the current known distance uh minimum distance between S to W is this W. This is the known shortest path between S and W. And also we have the shortest known path between S and V. In H relaxation we want to know that if we come from S to V and then V to W will give us a shorter distance or not. If it give us a shorter distance then we will update this this W. So this is called short uh uh edge relaxation. So we are trying to see if we use v to reach w will give us a shorter distance or not.
So if this plus weight give us a smaller number compared to this w then we updated this w with this uh actually number and also we update the predecessor also because now the predecessor should be v because we are reaching w through v. Let's see few examples.
If you understand this part, the rest is very easy. This is the most important part of uh Dextra algorithm. Suppose uh this is a dist array that showing um u minimum distance between source versus any other nodes in the graph so far.
And predecessor show that from which node we reach the destination.
And this is our graph part of the graph.
Suppose so far the shortest path that we find from S to U is five.
And here we can see that this is already in the distance array that shortest path so far to U is five and shortest path so far to W is infinity. Now we want to see if we go to W through U can give us a smaller number or smaller distance or not. So if you sum five + 7 it will be 12 which is smaller than infinity. So instead of infinity you will replace it with 12 and also uh you will update the predecessor that you access W through node u. So this called edge relaxation. So the previous shortest distance was infinity. Now we found an uh shorter uh distance compared to previous one. So we replace it 5 + 7 12 is less than infinity. So we replace infinity with 12 and update the predecessor. Any question here?
Okay. Another example.
This distance array said that so far the shortest distance to U is five.
Shortest distance to V is 8 and shortest distance to W is 12. Now we want to see if we come to W through V. It will give us a again shorter distance or not. So far the shortest distance between S and W is 12 that we can see here. Now if we come from S to V the shortest distance so far is 8 + 3 is 11 and 11 is less than 12. So we will replace 12 with 11 and also update U to V because now the shortest path known to V to W is true V.
So every time we have to update and find the shortest path and at the end we will have shortest path to versus each vertx in distance array and also we can use this predecessor to access and find the shortest path between any two um between source and any vertx. Any question here?
So this is the sudo code. We create distance array initialize it to infinity. We create predecessor initialize this to minus one. And also we have a v set that contain all vertics initially. And then we set the distance of source to zero. And also as long as we have a node that we are not processed and it is in v set, we select one of them that has a minimum distance so far and then remove it from v set and apply edge relaxation if it is applicable in that particular ed. um edge. So let's see example that it will be more clear that how it's all working.
Let me show you here.
So this is a graph and we want to find the shortest path between zero and any other vertex in this graph. So we want to find the shortest path between 0 to 1, 0 to 2, 0 to 3, 0 to 4 and 0 to 5.
The source is always fixed.
We initialize the distance array as I said always for the source we keep zero and the rest infinity and predecessor also we initialize with minus one. and we said have the list of the nodes in the graph. The initially we have all of this because we haven't processed it yet.
So this is a start point.
Now we want to start from zero. So we remove zero from v set and try uh and start exploring zero.
First uh we start from zero. We try to uh uh see or apply edge relaxation between 0 to one and then we do from 0 to two and then versus 0 to three. We check all other vertex that directly connected to the zero.
So we have to calculate uh distance the known shortest distance so far to zero plus the weight of the edge that take us to the one. So the shortest distance to zero is already zero if you check the distance array. So 0 + 14 will be 14. Then we have to check that if this 14 is less than known distance to one already. We see that in the distance array the shortest known path to what is already infinity and we found 14. But because 14 is less than infinity. So we update the distance and print. So now the shortest distance to one from the source is 14. And the node that we actually reach to one is true zero. So we update node one.
Next we have to check 0 versus two.
Now we want to check 0 versus two. We have to calculate um distance 0 + 9. So the distance 0 is 0 + 9 will be 9. We compare it with the uh minimum distance to two so far which is infinity. 9 is less than infinity. So we replace infinity with nine and then update predecessor also.
How about next one which is three distance 0 is 0 + 7 will be 7 and 7 is less than infinity. So we update node three also the shortest node from 0 to three so far is seven through vertex zero. So we processed zero node zero finished and now we should take another vertx and apply edge relaxation again.
Any questions so far?
So we process zero. Now we have to pick another vertex and uh repeat whole process. But which one we have to select? This is important. Should I uh next stage go to one, two, three, four or five? We have to look at the distance array and see which one has a minimum distance so far and we take that node.
So already zero is processed. We so we never check zero anymore. It's finished.
So the distance to one is 14 to two is 9 to three is 7 4 5 infinity. So between among all these nodes three has the shortest non distance so far. So next node that we have to process is node three. So this is how we select each node in dystra algorithm. So next is three. So we're removing tree from v set and start applying dextra algorithm on the node tree.
We want to explore node tree.
Okay. Three has three uh actually uh edges connected to three. Three to zero 3 to two and 3 to 5. We already explode zero. So we ignore this edge. You don't need to calculate this. If there is a edge to a node that already explored, you ignore it. You don't need to do all this calculation again. So we ignore this edge. You don't need to do any update or calculation here.
Next is sorry next is this edge 3 to two. So what you need to calculate is distance 3 + 10.
What is the known distance to three yet is seven? 7 + 10 17.
So then you check if 17 is smaller than or less than the known distance to three. We know that the shortest distance now so far is seven and this one is 17. So you don't need to update it because you didn't find a shortest distance. So no update here.
Next we have to have a look to this one three and five. So we have to again calculate this three + 15. This three is 7. 7 + 15 is 22 and 22 is larger than 7.
So no update again.
Oh sorry that was five. Five was infinity and this is 22. 22 is smaller than 15 infinity. So we update for node five.
So we explored node three also. So node zero and node three already explored and we do not check these two nodes anymore.
Now except for zero and three we have to check to find which one has the shortest distance and then we we will pick that note. So 1 is 14, 2 is 9, 4 is infinity and 5 is 22. Among these four, two has a shortest distance so far. So next node that we need to explore is node two.
Node two has four connection, four edges 2 to 0, 23, 2 one and 25. And we know that we already explored node zero and node three. So we ignore these two. We don't need to do any calculation for 220 and 223. So this one we ignore.
But this one we can do calculation because one is are not explored yet. So here we calculate distance 2 + 4.
Distance 2 is 9. 9 + 4 is 13.
And we know that the shortest path so far was 14 and this one give us 13. So we update it. We update 14 with 13 and then update the predecessor also. We are reaching to one through two. So we add two in the predecessor array.
Next is this one. We ignore this edge because see uh three is already explored. You don't need to check shortest part to three anymore.
And next is 2 to5. So known this shortest distance to two already is 9. 9 + 3 will be 11.
And 11 is less than 22. So we update shortest distance to five also. So it will be 12 and predecessor will be updated to two because we reach five through node two.
So we explored uh um node two also one two three is already finished. We have three more node one node four node five. Again we search and see which one has the shortest distance between 13 infinity and 12. We choose this one. So we next we explore five. So we remove five from the v set and start exploring five. Five has a edge to three to two and to four. Three is already explored. Two is already explored. So we ignore these two edges. We just need to check and see if we can find a smaller distance uh going to four through node five.
So this one we ignore. This one also we ignore. We just need to do calculation for this one. So distance 5 so far is 12. 12 to 8 is 20 and 20 is less than infinity. So we update for four. The distance now is 20 through node five. We update predecessor also.
So we explore node five also.
So 0 235 is finished. Between 1 and four the shortest distance is 4 1 which is 13. So next node that we need to explore is node one. Node one has three edges to zero to two and to four. Zero is already explored.
Two is already explored. And we just need to check four.
So this one ignore this one also ignore.
This is the only one. This one is 13. 13 + 5 is 18 and 18 is less than 20. So we update with the shortest path that we find which is 18 and predecessor is one.
So node one also finish. The last one is node four.
It has two connection. One is already explored. Five also explore. So done. No update here. And this is the end result.
The shortest distance between 0 to 1 is 13. 0 to2 is 9, 0 to 3 7, 0 to 4 18, and 0 to 512. And if you want to see the path, then you have to use the predecessor.
Let's see how we use the path and then we will see another example.
So here for example after you finish all if you want to say what is the shortest path between zero and four you start from four and have a look to the predecessor. So 4 to 1 and then from 1 to two and then two to zero. So from predecessor you find the path also 4 to 1 to two to zero is the shortest path and shortest distance also is in this array.
Any question here?
So last >> is there any path between two node?
measures >> though that's uh you that is just showing if there any path between two them but that is just telling you is if there any path but we are looking for shortest path between so these are two different problem >> no that's that itself doesn't give you this result if you want to update it add some few thing that is not the original version. Maybe you can develop an algorithm based on that. But that one itself no don't give you this.
That is just for unweighted and finding weight uh any path between them without um regardless of the weight.
So let's see another example and then okay let's do this one because this one has direction also uh we want to suppose one is the source and we want to find the shortest path between 1 to 2 1 to 4 and 1 to 3 and our graph is also directed. So we have to consider this direction also when we are applying the extra algorithm.
So let me skip it here.
So first we need to initialize all the data structure that we need.
One is this which shows I have to keep it like this.
The distance shortest distance between them.
Initially distance to one. Suppose one is our source is zero and the rest is infinity and then predecessor 1 2 3 4 which is minus one initially.
Okay. Now we want to start from node zero. So node one. So start from one. So we have to see all the directed neighbor or nodes that have a connection with one.
So we have connection between one to two sorry one to three with weight two. We have connection between one to four and one to two. Let's start with this one like 1 2 3 1 2 3 and weight is also two. So you have to calculate this one plus weight. So this one you already are on the source. So your distance to the source is zero plus the weight which is two. It will be two. Then you have to compare if the distance that you find to three is smaller than the distance you already have in the this array.
Currently this 2 three is infinity. Now it's two. So you can update it. It will be two and you reach to three via node one. So you update this one also to one.
So we already see this one. Next we want to explore this one. 1 2 4 and the weight is 10. Again this one plus weight this one is zero. The weight is 10. It will be overall 10. So the current this to four is infinity. And the weight that the distance that we found is 10. So we update it to the 10 and also predecessor to sorry one.
One the next. Okay. This one also we see. And now this one one to two and the weight is six. This 1 + w 0 + the weight is six. So overall is six. So the current distance to two is infinity. We found six. So we updated to six and through node one.
Any questions so far? So we processed node one. Now we have to choose two, three or four. Which one we want to do the next? Any question here?
So which one I need to choose for the next round?
>> Three. So now we have to process or explore the node tree because this one has the minimum. So let me put this and spread again here.
So we update from here.
One, two, three, four. This one is zero.
This one is six. Two and 10.
And the bread.
1 2 3 4 is minus one one one okay now because the wine is already processed uh among two three and four three has the shortest distance so we choose three we want to explore node three now we have to check all the connection of three has connection to one has to two and to four. One is already explored. So we ignore this one. You don't need to calculate this one again because node one is already explored. So we have to just check 3 to two and 3 to 4.
So let's start with 3 to two.
3 to two is three. Okay.
3 to two. And the weight is three.
Correct? Or three or two?
3 to two three. Okay. Now we say this three plus weight. Distance to three already is two plus weight which is three is five.
Okay. If five is less than two. No. So we don't update it because this is larger than two. So no update here.
Next is 3 to four and weight is 8.
3 to 4 and weight is 8. So distance 3 + w distance 3 is 2 + 8 is 10. So we check four.
Oh this one was to two. We should compare with this one. Yeah. So why you not saying anything?
You have to compare it with five is less than six. So this will be five and then we reach through node three. Okay, it is 12. We compare it with node 4 already is 10. This is 12.
So 12 is more than 10. No updates. So we explore node three also.
Node three finished. So we already did node one. We already did node three. Now we have to choose between two and four.
2 is five and four is 10. So next note that we explore is two.
So distance 1 2 3 4 0 5 2 10. And the distance and the bread is - one 3 one one. So next we are exploring node two. Let's see the connection of node two. Two has just one outgoing edge which is to four and four is not an explorer. So we have to check this one. 2 to 4 and the weight is four.
So 2 to four and weight is four. So this two plus weight. This two already is five and the weight is four which is nine and nine is a smaller than the current one. So we update it to nine and update the one to two. So the last is for itself. I think node four.
Four has a connection to two and two is already explored. So this finished. This is the final result.
This is the final result. This is the final distance array 0529. And this is the predecessor min -1 312.
Any question?
Just here because we have a direction we have to check the direction and then select the edges that uh actually have outgoing direction from a node that we are processing.
Do you need more example or is clear?
Yes or no?
No question.
You want me to do this one or is okay?
>> Yes.
>> Okay.
So let's do this one.
So the graph can be directed or undirected but it's weighted and weights are all non- negative. And here suppose the source is zero. And we want to find shortest distance between 0 to 1 2 3 and four.
Again we have distance 0 1 2 3 4 because zero is a source. So distance is zero. The rest is infinity and then predecessor for all of them initially minus one.
Okay, we start from zero node zero.
So zero has edge to one and two. First let's check this one. 0 to 1 and the weight is four. So this 0 + weight 0 + 4 is four. So already the distance to one was infinity. Now we found four. So we update it. and then minus one will change to zero. Next is 0 to 2 and weight is 8.
So this 0 + w this 0 is zero and the weight is 8. So overall eight. So distance to two we update it to 8 and this one zero. Now we already process node zero. Finish. Now we have to choose which node we want to do next.
Which one I choose?
Which one I need to choose for the next >> one.
>> One. Because one the distance is four.
two is eight and the rest infinity. So a smaller one is a four. So next node that you need to process is one. So next is node one.
So let me quickly put this and zero 1 2 3 4 0 4 8 infinity infinity at and print 0 1 2 3 4 is -1 0 0 - one - one. So we have to check node one. So check the connection of one. One has a edge to zero to two and four. Zero is already processed. We finished the zero. So we ignore this one. We don't need to do any calculation here. So we have to check one to two and one to four. One to two is three.
1 2 wait three. So this 1 + w this one is four.
W is three 7. So note two already is was eight. Now we found a shortest spot which is seven.
So we update it.
We replace it with seven and predecessor will be true node one.
The next was 1 to two. We already check 1 to 4 6 1 to 4.
1 2 4 6. So this 1 + w this one is 4 + w 6 10. So let's check four. Four was infinity. Now we found 10. So we update.
We put 10 here. True node one. So this one also finished. We already check node zero and node one. Now which one I have to choose?
Which one I have to check next?
Okay. Which one?
Zero already done. One already done.
Among two, three and four. This is seven. This is eight. This is 10.
I have to process node two.
So next we check node two.
Okay, node two has a connection to zero which is already processed. So we ignore this one. 2 to one already process.
Ignore this one. So 2 to three wait two.
two to three weight two.
So this two plus weight this two already is seven.
The weight is 2 9. So we check three.
Three was eight and now we found nine.
So no update because this is larger than the previous distance that we already calculated. So note two also finish no update.
Now between three and four three has the shortest distance. So next is node three.
Three has a edge to two. Two is already processed. So we ignore this one. We have to check this one. 3 to 4 10 3 to 4 and weight is 10. So this three plus weight this three is 8 weightight is 10 is 18.
So the current way distance to four is 10 and this one is 18. So no update. So node three also finished.
And the last is node four. Last always we don't do anything because four has a connection to one already processed. Three already processed. So this is the final result.
This one 0 4 7 8 10 and minus one 0 1 3.
Oh previous time I updated I have to put it here. When we put eight, we have to update it to two.
You can see better there. Why you not telling me we have to put two here, two here and yes, done.
Because whenever we update the distance, we have to update the predecessor also.
We reach to uh this node. So we have to update we reach it through zero. So this is zero.
We reach two through node zero.
Oh, where is it?
Where is it?
Oh, this eight was for two. Okay. No, this is this one. And what is this? Oh, this is infinity. Oh, we update it here.
Yes, correct. This is two. It was here.
Yes. Two. Yeah. Done. So this is the final distance 0 4 7 8 10 and the produces are min - 1 01 2 1.
Any question?
So if in the question they ask you how to find shortest path between two other vertx then you have to rerun this algorithm again because this one always find the shortest path between source and any other vertx. So if the source change you have to run all this algorithm again and recalculate all the distance and path and the next thing is uh uh the data structure that you use to save the vertics and then choose between them can be visited array can be a list or priority Q that we will discuss.
us in week eight next week. Based on which uh data structure you use to save vertx the complexity of whole model can be different. So let's see how it affect the final result because every time we choose one of the vertx from the vertx set and then we process that vertx. So based on how you save those u list of vertx it can affect the final complexity. For example, if you use visited array that we use for DFS and um BFS. So it can be true or false. So if a node is already processed, we change the uh actually uh the value to true. For example, if already you process node zero, then you put true here in visited.
It means you already processed the this node and you have to choose between the nodes that already not processed for the next uh round of the Dextra.
So because you have V node the complexity be OV because every time you have to check all these and choose the one that is already false and has a minimum distance also. So if you use visited array the complexity of choosing which node is the next will be o v.
If you use list also again the same for example initially your list say bcd and then you choose c as a next then your next round your unprocessed list will be b d. So again you have to choose and check uh among the node that already not processed which one has the smaller distance and pick that one. So even if you use list it will you have to check one by one is like a unsorted array or unsorted list that you have to every time uh search from first of item in the list and check which one has a smallest distance and pick that one as a next uh actually a note or you can use priority Q that we will discuss uh in week nine.
In this case that we will see how it works. Then uh it will reduce the complexity because in priority uh Q uh we insert based on the priority. For example, here the priority is with the one has a smallest distance. It will uh decrease the complexity to log N. But we will discuss in week nine how actually priority Q work.
So overall the complexity of dystra we every time we have to consider each edge. So the complexity the first stage is OE because we uh check each edge that connected to the current node in the worst case it can be OE and then we have a loop in each round we will choose uh the one node that has a shortest distance. So then inside of the loop each time we uh choose a node with the shortest distance. So overall the complexity will be E + square V. If you use list or array again will be E + square V or if you use priority Q then it will be um the choosing the item will be log V.
So overall the cost will be OE plus V log V. So I can maybe show you better on the code itself.
Where is Yeah, here.
So this is how you this is the uh the loop that um your loop is based on the number of vertex that you have. As long as there is a vertex in the visited array, this uh line will be repeat and each time you have to find one node with the shortest distance. So this loop itself uh is v and then searching to find the one has a shortest distance based on which uh data structure you use to implement the v set. Then it can be OV or it can be log N and give you the final complexity.
The only difference is what data structure you are using to save uh the V set.
Uh we have some other version we do not discuss in this course. One is Floyd Marshall which can be used for negative weights also and also Bellman Ford another algorithm that also can be used for negative weights but Dextra itself uh applicable if you have just positive weights that's it this is about dystra to find shortest path uh for single source node in a graph.
Any question about Dystra?
Okay, maybe you can have 5 minutes break and then we start other topic.
Yes.
>> Yes. One moment.
>> I want to put this Okay, they just asked one good question.
If uh there are two or three nodes with the same distance, just choose one of them arbitrarily because always I say choose the one that has a smallest distance as a next note to process. If there is two or multiple nodes that already have similar shortest distance, just choose one of them. There is no difference on the final result.
Okay.
Uh next is minimum spanning tree. We already discussed about this spanning tree of a graph. A spanning tree of a graph is a tree that has all the nodes of the graph but subset of the edges of that graph. So that subset of the edges that you choose you should choose such a way that it doesn't create a cycle. This is what we know from previous uh sessions. Now we want to find minimum spanning tree. So in case our graph is weighted and then we want to create spanning tree how we should choose the minimum spanning tree. So a spanning tree that has a overall minimum cost.
We have two algorithm for this. We have many but these two we will check uh discuss today cross call and p quite simple.
So we already know that for undirected graph uh to create a spanning tree we choose all the vertex and a subset of edges. We have two condition.
Uh first there is no cycle in a spanning tree and second between each pair of the nodes you have to have a path. So they should be connected or reachable. But what if we have a weighted graph? In weighted graph your spanning tree also need to be we are looking for minimum spanning tree. So the overall if you sum all the weights overall data spanning tree should be minimum also with minimum cost or minimum weight. For example if this is your graph both of them are a spanning tree because as you can see we include all the edges all the vertics and we choose a subset of edges they are all connected. It means if you choose any pair of the nodes there is a path between them and there is no cycle. Here you can see this both condition apply and here both condition apply. But this one is minimum spanning tree because if you sum all these numbers or cost 2 4 6 7 8 9 the total cost of this spanning tree is nine and this one is 11. So both are a spanning tree but this one is minimum spanning tree and today we want to use those two algorithm crosscall and p to see how we can find minimum spanning tree of a graph a weighted graph and one important things is each graph can have multiple spanning tree or multiple simple minimum spanning tree. So their their shape can be different uh but the minimum spanning tree all of them have the overall same cost but their edges that you choose can be different. Especially if you have a graph with multiple age with the same weight then you may have different minimum spanning tree and all of them are correct.
So this is again the cross call is the person who invented this algorithm.
Let's start with crosscall. Quite simple process to create minimum spanning tree.
We start with an empty graph. So we have a graph that has just nodes no edges.
And then we will sort all the edges ascending based on their weight from the shortest smaller weight to the largest weight. And then every time we choose the edge with a smaller weight and add it to our graph. We continue as long as there is no it this adding this edge cannot uh doesn't create any cycle. And we continue until we add v minus one edge into the graph. V is the number of vertex in spanning tree. Always the number of edges is uh less than number of nodes.
So let's see how it works.
Uh suppose this is our graph and we want to find minimum minimum spanning tree.
So I prefer to do it this way is easier. Then I will show you on the graph. First thing we create an empty graph.
0 1 4 3 and two. No edges. Then you find the edge that has a minimum weight. You may have two, three, four edge with minimum weight. Just choose one of them. Here there is no edges with similar weight.
But if you see edges with similar weight, just choose one of them random.
So here this is the edge that has a minimum weight. So first I add this one.
So which one is the next?
this edge because it weight is two. I will check if I add this uh will it create a cycle or no? No, because steel is a sparse. So we add this one also. We continue until we add v minus one edge into the graph. So we have five vertics. So we need four edges. What is the next?
Which one I have to choose next?
0 to three.
We added because it doesn't create a cycle and also still we have space to get more edges. What is the next?
Which one has the smallest weight?
0 to can I add 0 to four?
>> No. Because it create cycle. So no. What is the next?
We cannot select this one also because if I add this one again it will create cycle. So this one also. No. Next three to two done finish. Because we have a five vertex we can maximum add four edges no cycle and also there is a path between every pair of the nodes. So this is correct.
This is the minimum spanning tree of this graph. So we started from empty graph then we start adding edges in ascending order. We add v minus one edges and every time we have to check is there if it create cycle or not. If it create cycle we ignore it and then check the next one.
So this is the final minimum spanning tree.
Any question?
So let's see in the slide. So started from here we add this one and then this one and then this one.
Four we cannot add five we cannot add and then six. So this is the final result.
So this is the final minimum spanning tree of this graph.
So as you can see this is the pseudo code.
Input is a graph. Output is minimum spanning tree. Start with a empty graph which has all the vertex.
And then we sort all the edges ascendingly based based on the weight.
And for each edge we add to the MSD and then check if it is create cycle we remove it and we continue until we have V minus one edges in the minimum spanning tree.
This is what we did and we can have another version. One is checking if it create a cycle or another version is again we'll start from empty graph. We sorted all the edges and then we when we want to add one edge to our MSD we will check if if there is a path already between V and W we do not add that edge. So for example this one we ignore 0 to 4 because already there is a path between 0 to four. We do not add this edge. So instead of checking cycle it says checking cycle is if I add this edges and then check if there is a cycle. But the second algorithm do not add the edge. Just check if there is a path between the source and the destination.
If already there is a path you do not add this edge at all because it will create a cycle. So do two different perspective but the uh outcome the results are the same. The first one we add and then check if create a cycle. If it's create we remove it. Second one we do not add. We first check if there is a path between the first and second node.
If there is a path we do not add. Any question.
So uh this is a proof on saying that there is no any algorithm that can find a minimum spanning tree with uh overall weight less than what crossall can find. So if crosscall find a minimum spanning tree this is real the minimum one that can be find this guarantee that there is no any other spanning tree with overall weight less than what kal already find for us.
So this is just a proof. Um I quickly go through that how we can prove that uh that minimum spanning tree by crosscall is actually minimum and it's guaranteed that there is no better algorithm than corros to find minimum spanning tree.
Suppose we have a graph and K is the tree that we generated using crall algorithm and A is another one that generated using another algorithm and both of them are minimum spanning tree. Then we will check the tree that already created by cross call algorithm and we find if there any edge that exist in cross al spanning tree but is not exist in the other one that generated by another algorithm and then add that edge to the spanning tree and then remove the highest weight age. We will see in the example and then at the end we will see that with doing repeating this process the second tree will be like the cross skull tree.
Just imagine here this is the graph and this is all the edges in this graph and this one two three are weight of the edge. This is the graph and then this is the minimum spanning tree that crossall algorithm find. So in crossall it find that this if you include these edges it will give you the minimum spanning tree and this is a minimum spanning tree through another algorithm.
So first we find one edge that appeared in corrosol but is not in the other spanning tree. So here this one so it is more large this graph. So as you can see there is three dots. So it's a larger just a part of a graph. So we found one uh edge that is appeared in cross call but is not appear in the second minimum spanning tree. We find this edge E5 is here.
So we add it in the tree that generated through the other spanning tree algorithm we add. And now we can see that there is a cycle here. And if you check here, this edge is E4, E5, E1, and E8. First, we add this edge here. And then we remove the edge that has the highest weight. So in this E8 has the highest edge. So we remove it.
So it will be like this. As you can see if you continue this process you can see that this tree will be more look like cross call tree to original minimum spanning tree that generated but the other algorithm. See here this was the tree that generated by cross call.
This one is was generated with other spanning tree with adding the edge and removing the one that has a highest weight. We can see that this second tree also look like the cros tree. So it's proved that and if it continue more and more it will be identical with corrosol one. So it shows that there is no uh minimum spanning tree algorithm that can find a minimum spanning tree with lower cost of crosscut and complexity.
Initially we need to sort all the edges.
uh for example if you merge sort or quick sort in an average or hip sort will give you e log e hip sort you will see in week nine. So sorting minimum cost is e log e this is the first thing is a fixed because you always need to sort all the edges and then choose the one with minimum weight.
So this part is e log e and then we have a main loop. That main loop uh actually run e times and e is the number of edges and every time we need to check if adding this edge create a cycle or not.
So for example if we use DFS to check if there is a cycle or not or whatever algorithm we are using it is affect the final complexity of this model because every time you add a edge and then check if there is a um cycle or not. Imagine we are using DFS then the complexity of finding cycle will be O V.
Why it is OV? It is not V + E you think because the complexity of DFS is actually V + E remember because this is a spanning tree and number of edges is always less than number of nodes and because in big O always we take the dominant part so we can ignore E here because E is always smaller than V.
So the total cost will be E log E which is for sorting edge and then main loop which is E mult* V. So overall we get the most dominant part which is O E multiply V. So this is the overall complexity of crosscall algorithm.
If you consider corrosol algorithm uh we have another way to reduce this and make it uh more simple to reduce the complexity.
Uh another way that we do not discuss in detail is using uh union find instead of cycle check. In this case we suppose that we have a connected component and if the node that you choose are from same connected component we do not add the edges because it will create a cycle. So this is per this different way of checking if there is a cycle or not.
So every time it will check if these two node that you are selected and want to add the edge are in the same corrected connected component or not. So in this case the uh complexity is O alpha V which is inverse occurment function which is in worst case can be a constant number. So it can say the complexity can be O1.
So in this case then the overall cost will be for sorting plus E because the cycle check now is constant. So it will be E log E plus E. If we choose the most dominant part complexity will be E log E that we can instead of E log E say E log V because we know that the number of edges always less than square V. So we can say log e is almost equal to log v. So we can say e log v as a final complexity of c call algorithm.
Any question here? Just have a look to this number formula.
Do you want another example for this algorithm or is he or you want to do another example?
>> Yeah.
>> Okay. So suppose this is the graph and we want to make minimum spanning tree of it. So let's see if I can do here or Yeah.
No. Where is it? No. Here zero.
One.
2 3 8 7 6 5 and 4 5 6 7 8 9. So we have nine vertics. So we need eight edges.
One, two, three, four, five, six, seven, eight, nine. Okay, we start from the cheapest one. The lowest weight.
Which one has the lowest weight?
This one. Seven to six. So I add this one here.
One. Now I need seven more.
So when which one is the next?
We have 6 to5 with cost 2 and 8 to two with cost two. You can choose whatever you want. This one or that one. No difference. So let's choose this one.
Two.
We need six more. What is the next?
is this one because it's not creating cycle I'm just adding so I need five more which one is the next 0 to one is option two to five is also option so let's get this one four we have four we need four more I can choose this one also because this is a minimum again compared to rest and it doesn't create a cycle. So I can add this one also four. I need three more.
Okay, we use this one also.
Now this is 10. This is 14. This is 9. 7 6 7 8. So this is the shortest smallest one. So can I add this one?
>> No.
>> No. Because this is create cycle. Next smallest one. This 7 to 8. Can I add?
>> No. Because it creates cycle. Next is 2 to three. Can I add? Yes. Because it doesn't create cycle. So I need two more. What is the next minimum?
We have eight here and eight here.
Can I add this one?
It doesn't create.
Can I add this one? No.
Uh, next is this one. Can I add this one?
Done. Finished.
Because I add one, two, three, four, five, six, seven, eight edges. the and you can see there is no cycle and there is a path between any pair of the nodes.
So this is the minimum spanning tree of this graph.
As I said mi based on for example uh you if you have a equal edges with equal weight based on you choose which one you may see that final tree has a different shape but the cost is still minimum. So doesn't matter maybe two of you try this but one of you choose this one first and another choose this one first. So final three the shape can be different but the weight both of them will be the same and both of them will be minimum spanning tree.
Any question?
So let's see prim also how prim find the minimum spanning tree.
So p is developed by these two people yarn and p.
The first one is developed and the second one rediscover it again.
But is based on the second one name interesting. Okay. Uh p also find minimum spanning tree here also we but the approach is different uh start with an empty graph.
So when I say empty graph even not any node starting point then we start from any vertex we have to choose one vertex and a start uh from that vertex that can be any vertx random you choose one vertx and then build your u msd start from that vertex. We just get one vertex and add it to our MTMS MST and then we find the cheapest edge that is connected to this vertex and any other vertex. The condition is when you choose a edge one side should be connected to the vertex that are already in MSD and one side should be a vertex is not in MSD yet and it should have the lowest cost also. So there is two condition here. You first choose a vertex. Then you need to choose a edge.
That edge one side of it should be in the MSD. One side of it shouldn't be in the MSD and it has a minimum weight.
And then you choose and continue until you add V minus one edges in your minimum spanning TD and again no cycle.
low see what does it mean?
Uh okay here if I want to be like what we explained the first stage minimum spanning tree nothing now I will choose one vertx for example we choose zero then we choose we check all the edges then one side is select is connected to zero and one side is connected to the nodes that are not in the minimum spanning tree yet. So it means this one, this one and this one. So with among these three which one has the minimum weight zero to one. So we expand our MSD now will be zero and one and weight one. So we creating this MSD gradually. But in chorus we already have all the notes and add edge. But here the approach is different. Now this is important here.
Let me change the color.
Already in MSD we have zero and one. Now we want to check all the edges. Then one side is connected to zero or one and other side is a node that is not in MST yet.
So which edges I need to check now?
I can check this one because one side is connected to zero and one side is connected to the node that is not in MST yet. I can check this one because one side is connected zero and one side is not connected to the is not in MST. Next is this one.
One side is connected to one and one side is a node that is not in MST and next is this one. Now among these four which one is minimum 0 to three.
Now which edge I need to check?
Now I have 0 1 and three in the MST.
Four and two are not in the MST. So I should find the edges. Then one side is connected to zero, one or three and other side is four or two. So which one?
It can be this one again.
Correct? because one is connected to zero and one is four.
Next can be this one again because one side is connected to one, one side is to four. It can be this one again.
One is connected one, one is to two.
It can be three to four also. It can be three to two also. Now among them which one is the smallest one?
This one sorry like this. Now again we have to find edges that one side is either connected to 0 3 4 1 and other side is two.
So which which one is the next with this one six. So this will be the end because already 2 4 5 I selected four edges. Uh there is a path between any pair of them and there is no way to add more. So this is our spanning tree.
So now look at here. First we add zero.
So we have three options. One, four or three. Among these three, one is the smallest one. So we choose this one.
Now we have four options. This one, this one, this one, this one. on one side is connected to the nodes that are in the uh MST and other side are not in the MSD. Between these four again I have to choose the smallest one which is this one.
Then I have more options. This one, this one, this one. This one and this one.
Again we choose this one.
And then the last one we choose this one and done.
This is this will be the final result.
This is how prime works.
So start from empty graph and then this uh among the edges in the graph we have to choose the edge that one side is in the MST one side is not in MST and then we check uh every time that we choose one we have to check if Um we we already choose the vertx. Now we have to find the cheapest edge among all the edges that are possible to be choosed. And we select the cheapest one. Add that one to our MSD.
And we can use one another data structure that tell us which edge already being used and which edge is remaining. So this one actually save the edges that already being added to our MSD and and every time we add it to the MSD we should remove it from that used edge that edges that one side is connected from connected to the MSD and connected one side is not connected to MSD.
So we have to consider every edge and then our loop is based on number of nodes that we have. Then when you we choose a node then we have to uh find the cheapest edge connected to that node um which is OE because just imagine that edges are in unsorted array. So you have to check one by one to find the cheapest or the one with the minimum weight. So overall cost OE is um to find one edge is connected to um MSD and one is not connected to MSD and our loop is V multiply E that E is the edge with the minimum weight. So overall will be E plus V E and then more dominant part is V E.
We can make it better with Fibonacci heap that will be in the week 9 in this case then it will be E + V log V instead of E. So it can be cheaper.
It depends on which data structure we are using to find uh the minimum weighted edge.
But the current sudo code is ov multiply e that what we see now.
Okay. Do you want to do again for frame also quickly?
This is quite large but too much option.
Okay, let's do at least few part of it or okay I can do on this. So suppose I want to apply p you help me. Suppose we start we choose a because if I draw it again it will be too much. Now what are the options?
I started with A and I want to use P and find the minimum spanning tree.
What are the candidate edge here?
A to B, A to C and A to D. These three are candidate edges here. So the one that has a minimum weight is A to C. So we choose this one.
So already our MSD has two nodes and one edge. Now we have to check another candidate set of candidate. So four.
So you have to have a look to this one to this one. No, sorry. Uh not this one. So we have to one is this, one is this, one is this one, one is this one, one is this one, and this one. These are the edges that one side is connected to A or C and other side is some other nodes. So among these six candidate we have to choose the one that has the lowest weight.
This is four. This is two. 7 5 11 8. So this one has the lowest weight. So we choose this one.
Now our MSD has A C B edge A to C and edge B to C.
Then we need to check all the other candidate. This one cannot be is because if I consider this one make cycle. So this is the option.
This can be option. This can be option.
This is again option. And this is again option.
6 8 11 5 and 7. So this is the smallest one. So we need to proceed like this. Every time we see uh find the all the candidate age, one side is connected to MSD, one side is not connected to MSD and then choose which one has the minimum weight and add that one to our MSD.
And then we continue like this. Any question.
Okay, that's it.
And I think I'm not sure. Let's check if assignment two is already published. Assignment.
Yes.
Okay.
this assignment to also you can have a look maybe we can quickly go through next Tuesday and explain how it works.
Okay, thank you.
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

WOW! Judge TURNS THE TABLES on Trump in His OWN $10B LAWSUIT!!!
MeidasTouch
197K views•2026-07-23

Playstation NO DISC/NO BUY Fight Is Over...
DavidJaffeGames
4K views•2026-07-23

Steam and Xbox Just Dropped The Hammer On PlayStation
OhNoItsAlexx
9K views•2026-07-23

Americans Confused in Australia for 17 Minutes Straight
IWrocker
17K views•2026-07-23