Computer Science Lecture Notes - Algorithms and Data Structures
Computer Science Lecture Notes - Algorithms and Data Structures
Introduction:
● Shortest Path Problem: Finding the minimum path cost between nodes in a
weighted graph.
● Applications: Network routing, pathfinding in AI, transportation logistics.
Dijkstra's Algorithm:
● Assumptions:
○ The graph has non-negative edge weights.
○ The graph can be directed or undirected.
1. Initialization:
○ For all nodes vvv in the graph:
■ dist[v]=∞dist[v] = \inftydist[v]=∞ (estimated distance from source to
vvv).
■ prev[v]=undefinedprev[v] = \text{undefined}prev[v]=undefined
(previous node in optimal path).
○ Set dist[source]=0dist[source] = 0dist[source]=0.
○ Create a priority queue QQQ containing all nodes.
2. Algorithm Execution:
○ While QQQ is not empty:
■ Extract node uuu with the smallest dist[u]dist[u]dist[u] from QQQ.
■ For each neighbor vvv of uuu:
■ Relaxation Step:
■ Calculate alt=dist[u]+w(u,v)alt = dist[u] + w(u,
v)alt=dist[u]+w(u,v).
■ If alt<dist[v]alt < dist[v]alt<dist[v]:
■ Update dist[v]=altdist[v] = altdist[v]=alt.
■ Update prev[v]=uprev[v] = uprev[v]=u.
■ Decrease the key of vvv in QQQ to reflect the
new dist[v]dist[v]dist[v].
3. Termination:
○ After processing all nodes, dist[v]dist[v]dist[v] contains the shortest
distance from the source to vvv.
○ The shortest path can be reconstructed by following prev[v]prev[v]prev[v]
from the destination back to the source.
Implementation Details:
● Priority Queue:
○ A min-heap is commonly used.
○ Efficiently supports extract-min and decrease-key operations.
● Data Structures:
○ Adjacency List: Preferred for sparse graphs.
○ Adjacency Matrix: May be used for dense graphs but less efficient for
Dijkstra's algorithm.
● Using Min-Heap:
○ Initialization: O(V)O(V)O(V).
○ Each edge is relaxed at most once: O(E)O(E)O(E) decrease-key operations.
○ Total complexity: O((V+E)logV)O((V + E) \log V)O((V+E)logV).
● Using Fibonacci Heap:
○ Decrease-key operation is O(1)O(1)O(1).
○ Total complexity: O(E+VlogV)O(E + V \log V)O(E+VlogV).
● By Contradiction:
○ Assume there's a shorter path not found by Dijkstra's algorithm.
○ Show that this leads to a contradiction with the way nodes are selected
based on minimum distance.
Optimizations:
● Bidirectional Dijkstra:
○ Runs two simultaneous searches from the source and the destination.
○ Can reduce the search space significantly.
● A Search Algorithm*:
○ Incorporates heuristics to guide the search.
○ Used when the goal node is known and a heuristic estimate of the distance
to the goal is available.
Example Implementation:
● Pseudo-code:
plaintext
Copy code
● OSPF Protocol:
○ Uses Dijkstra's algorithm to compute the shortest path tree for routing
decisions.
● Traffic Engineering:
○ Adjusting weights to balance network load.
Assignments:
● Programming Task:
○ Implement Dijkstra's algorithm using a binary heap and test it on a large
dataset.
● Theoretical Exercises:
○ Prove that Dijkstra's algorithm always finds the shortest path in graphs
with non-negative weights.
○ Modify the algorithm to handle graphs with zero-weight edges.