Memory
Memory
1
CSE 241: Algorithms
Introduction
Memory is essential for storing and
retrieving data.
Types:
● Primary Memory
● Secondary Memory
● Cache
2
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.
3
CSE 241: Algorithms
Relaxation
2
4
2
1 3
Source
4
CSE 241: Algorithms
Relaxation
2
2
4
2
∞
1 3
Source
5
CSE 241: Algorithms
Relaxation
2
2
4
2
1 3 2+4=6
Source
6
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)
}
7
CSE 241: Algorithms
Dijkstra Algorithm -> Greedy
7
2 4 1
2
1 1 2 6
4 5
3 5
3
8
CSE 241: Algorithms
Dijkstra Algorithm
2
∞
7
2 4 1
2
∞
1 1 2 6
Source
4 5
3 5
3
4 ∞
9
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.
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
11
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
12
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
13
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
14
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
15
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.
16
CSE 241: Algorithms
Bellman Ford - Dynamic
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.
17
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<∞
18
CSE 241: Algorithms
Bellman Ford
10
R2:
B
10
-8
A
5 C
Source 5
2
19
CSE 241: Algorithms
Bellman Ford Negative edge Cycle
10
R3:
B
10
-8
A
5 C
Source 5
2
20
CSE 241: Algorithms
Bellman Ford Algorithm
Cycle Detection:
Complexity:
● Time Complexity: O(V * E), where V is the number of vertices and E is the
number of edges
21
CSE 241: Algorithms