CST370 Week 8
This was our eighth week in CST 370 - Algorithms.
Dijkstra's Algorithm
The last algorithm that we covered in this class was Dijkstra's Algorithm.
It is a greedy algorithm that solves the single-source shortest path problem efficiently.
To do the algorithm we store a table that has number of vertices rows and number of vertices columns. In the first row we write down the cost to get to each vertex directly from the root, and infinity for any vertex not directly connected to it.
Then, we visit the node with the lowest cost from the root (this is the greedy part). Then we recalculate the cost for every node that is connected to the current node, combining the cost to get to the current node to the cost to get to each connected node from the current node. We repeat this process until we have visited every node. When writing down the cost to visit each node, we also write down which node we are coming from.
Finally, we can get the optimal path if we start from the last node and work backwards to the root node.
Comments
Post a Comment