Core Concepts: G (V, E) V E
Core Concepts: G (V, E) V E
Graphs: A graph G = (V, E) consists of vertices (nodes) V and edges E , which connect pairs
of nodes. Edges can have weights representing distances, costs, or time between the nodes.
Graphs can be directed (edges have directions) or undirected (edges don’t have directions).
Weighted Graphs: In a weighted graph, each edge has a weight associated with it, which often
represents the “cost” to move from one node to another. Dijkstra’s algorithm works with
positive weights.
Path: A path in a graph is a sequence of vertices connected by edges. The length or cost of a
path is the sum of the weights of the edges along the path.
1
CSE 241: Algorithms
Core Concepts
Shortest Path: In the context of graphs, the shortest path between two nodes is the path that
has the minimum total weight. Dijkstra’s algorithm finds the shortest path from a single source
node to all other nodes in the graph.
Priority Queue: A priority queue is a data structure that allows for efficient retrieval of the
smallest (or largest) element. Dijkstra’s algorithm uses a priority queue to keep track of the
current shortest distances as it explores the graph.
Relaxation of EDGE: This technique is key to shortest path algorithms. When an edge from u to
v with weight w is relaxed, it means updating the shortest known distance to v by checking if
going from u to v provides a shorter path than previously recorded.
2
CSE 241: Algorithms
Relaxation
2
4
2
1 3
Source
3
CSE 241: Algorithms
Relaxation
2
2
4
2
∞
1 3
Source
4
CSE 241: Algorithms
Relaxation
2
2
4
2
1 3 2+4=6
Source
5
CSE 241: Algorithms
Relaxation
2
2
4
2
u
6
1 3
v
Source
if ( d[u] + c(u,v) < d[v] ){
d[v] = d[u] + c(u,v)
}
6
CSE 241: Algorithms
Dijkstra Algorithm -> Greedy
7
2 4 1
2
1 1 2 6
4 5
3 5
3
7
CSE 241: Algorithms
Dijkstra Algorithm
2
∞
7
2 4 1
2
∞
1 1 2 6
Source
4 5
3 5
3
4 ∞
8
CSE 241: Algorithms
Dijkstra Algorithm
2
∞
7
2 4 1
2
∞
1 1 2 6
Source
4 5
3 5
3
∞
2+1=3
Choose the one with the smallest Distance. So choose node 2 first.
9
CSE 241: Algorithms
Dijkstra Algorithm
2
2+7=9
7
2 4 1
2
∞
1 1 2 6
Source
4 5
3 5
3
∞
2+1=3
10
CSE 241: Algorithms
Dijkstra Algorithm
2
2+7=9
7
2 4 1
2
∞
1 1 2 6
Source
4 5
3 5
3
2+1=3 6
11
CSE 241: Algorithms
Dijkstra Algorithm
2 8
2+7=9
7
2 4 1
2
∞
1 1 2 6
Source
4 5
3 5
3
2+1=3 6
12
CSE 241: Algorithms
Dijkstra Algorithm
2 8
2+7=9
7
2 4 1
2
11
1 1 2 6
Source
4 5
3 5
3
2+1=3 6
13
CSE 241: Algorithms
Dijkstra Algorithm
2 8
2+7=9
7
2 4 1
2 9
11
1 1 2 6
Source
4 5
3 5
3
2+1=3 6
14
CSE 241: Algorithms
Dijkstra Algorithm
Time Complexity:
When using an array or list the time complexity is O(V2) where V is the number of
vertices.
Finding the minimum distance node takes O(V) time, and there are V such nodes
to process, leading to O(V2). This is feasible for small, dense graphs.
15
CSE 241: Algorithms
Bellman Ford
Purpose:
● Finds shortest paths from a single source node to all other nodes.
● Works on graphs with negative edge weights.
Algorithm Process:
● Initialize distances from the source to all nodes as infinity (∞), except the
source, which is set to 0.
● Relax each edge repeatedly for (V - 1) times, where V is the number of
vertices.
● If a shorter path is found during relaxation, update the path cost.
16
CSE 241: Algorithms
Bellman Ford R1: 10<∞
∞ 10
B 10
B
10
A
-8
A
-8 ∞-8=∞,
So, no relaxation
5 C 5 C
Source ∞ 5
5<∞
17
CSE 241: Algorithms
Bellman Ford
10
R2:
B
10
-8
A
5 C
Source 5
2
18
CSE 241: Algorithms
Bellman Ford Negative edge Cycle
10
R3:
B
10
-8
A
5 C
Source 5
2
19
CSE 241: Algorithms
Dijkstra Algorithm
Cycle Detection:
Complexity:
● Time Complexity: O(V * E), where V is the number of vertices and E is the
number of edges
20
CSE 241: Algorithms