One Pager Cheat Sheet
- The lesson covers
Dijkstra's algorithm
and its application in solvingshortest path problems
, including its implementation in Python and comparison to other methods likeBreadth-First Search (BFS)
,A* search algorithm
, andFloyd-Warshall algorithm
. - The shortest path problems are represented using a
graph
, requiring a solid understanding ofgraph concepts
. - The graph in mathematics and computer science comprises two key components: vertices or nodes which represent points of connection, and edges which represent the connection between these points, and it can be
directed
orundirected
; any missing options in the multiple choice test notwithstanding, the accurate answer must include both vertices (nodes) and edges. - In shortest path problems,
nodes
represent cities andedges
represent connectivity, with paths determined from one or moresource nodes
, using eitherdirected
orundirected
graphs. - In computer science, a graph is a collection of nodes and edges, with directed graphs featuring unidirectional edges, resembling one-way streets, while undirected graphs feature bidirectional edges, similar to two-way streets, with
unidirectional
andbidirectional
describing the nature of node connectivity in these graphs. - The problem representation requires the input graph to have one source node and non-negative weights for
Dijkstra's algorithm
, which finds thedistance values
for the shortest path from the source node to each unvisited noden
, aiding in determining the shortest path between complex nodes, such as hundreds of cities. - The Dijkstra's algorithm is implemented by initializing variables such as a list of
unvisited
nodes, a list ofdistances
to nodes (with all initially set toinfinity
except the source node set to0
), and a list ofvisited
nodes that form theshortest path
; the algorithm, beginning with a source node, adjusts distances of nodes in this context. - The algorithm progresses by moving to the unvisited node with smallest distance, node
C
, and updating the distances of nodesD
andE
to6
and10
respectively, after finding that the sum of the source-to-node distances for these nodes is greater than the original distance. - The
unvisited node
with the smallest distance value,B
, is moved to thevisited node list
, but as visitingB
creates no new paths, no distance values are altered andB
is not added to the path. - The unvisited node with the smallest distance value, node
D
, is visited to discover a new connectionF
, which has its distance value updated to10
as this provides the shortest path from the previous nodeC
. - The algorithm proceeds in lexicographic order, visiting node
E
first, which does not alter the shortest path or the distance values of the other nodes. - The shortest path to destination node
F
is completed by adding the path from nodeD
toF
, resulting in a total weight of 10, which is smaller than any other path toF
. - Dijkstra's algorithm can accurately calculate the shortest path in both directed and undirected graphs, by considering the least cumulative weight from the start node and treating
edges
in undirected graphs as two separate directed edges. - Dijkstra's algorithm doesn't depend on the direction of edges and can represent the graph as an
adjacency list
using nested dictionaries. - The unvisited, path, and distance lists of nodes are initialized, setting the initial distance values of all nodes to
infinity
except the source node which is set tozero
. - The algorithm selects the
unvisited
node with the shortest distance from the source, updates distances by checking all theneighboring
nodes, adds nodes to the path list when distance is updated, and repeats this process until every node in the graph isvisited
. - To find the shortest path between nodes, the reachability of the
destination
node from thesource
node is checked, after which the destination is added to the route list if reachable. - Time complexity is an
important tool
that helps us estimate the efficiency of an algorithm, identifybottlenecks
in the code, calculate the time taken to execute its steps, measure the speed of a process, and determine the optimal solution. - Dijkstra's algorithm has a O(E log V) time complexity using adjacency lists, which is better than brute force algorithms.
- Dijkstra's algorithm is a
fundamental
concept for understanding shortest path problems, and will be compared to other algorithms in the next part of the tutorial.