0% found this document useful (0 votes)
2K views

Computer Science Lecture Notes - Algorithms and Data Structures

Uploaded by

Fiuͥseͣnͫ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Computer Science Lecture Notes - Algorithms and Data Structures

Uploaded by

Fiuͥseͣnͫ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Topic: In-Depth Analysis of Dijkstra's Algorithm

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.

Detailed Algorithm Steps:

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.

Time Complexity Analysis:

● 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)log⁡V)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+Vlog⁡V)O(E + V \log V)O(E+VlogV).

Correctness Proof Sketch:

● 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.

Limitations and Alternatives:

● Negative Edge Weights:


○ Dijkstra's algorithm fails with negative weights due to potential infinite
cycles of decreasing path cost.
○ Alternative: Use Bellman-Ford algorithm for graphs with negative weights.
● Dynamic Graphs:
○ For graphs where edge weights change frequently, re-running Dijkstra's
algorithm may be inefficient.
○ Alternative: Use algorithms designed for dynamic graphs.

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

● function Dijkstra(Graph, source):


● dist := array[0..number of vertices] of float
● prev := array[0..number of vertices] of vertex
● for each vertex v in Graph:
● dist[v] := infinity
● prev[v] := undefined
● dist[source] := 0
● Q := priority queue containing all vertices of Graph
● while Q is not empty:
● u := vertex in Q with min dist[u]
● remove u from Q
● for each neighbor v of u:
● alt := dist[u] + length(u, v)
● if alt < dist[v]:
● dist[v] := alt
● prev[v] := u
● decrease-key v in Q
● return dist, prev

Applications in Network Routing:

● 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.

You might also like