0% found this document useful (0 votes)
2 views21 pages

Memory

The document provides an overview of algorithms related to graph theory, specifically focusing on Dijkstra's and Bellman-Ford algorithms for finding the shortest path in graphs. It explains key concepts such as relaxation, priority queues, and the time complexities associated with each algorithm. Additionally, it discusses the handling of negative edge weights and cycle detection in the Bellman-Ford algorithm.

Uploaded by

pagladashu47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views21 pages

Memory

The document provides an overview of algorithms related to graph theory, specifically focusing on Dijkstra's and Bellman-Ford algorithms for finding the shortest path in graphs. It explains key concepts such as relaxation, priority queues, and the time complexities associated with each algorithm. Additionally, it discusses the handling of negative edge weights and cycle detection in the Bellman-Ford algorithm.

Uploaded by

pagladashu47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CSE 241: Algorithms

Md Taukir Azam Chowdhury


Lecturer, CSE
BUBT

1
CSE 241: Algorithms
Introduction
Memory is essential for storing and
retrieving data.

Computers use a hierarchy of memory for


efficiency and cost management.

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

No Direct Path to 3. So we don't know the path and initialized as Infinity.

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

Choose the one with the smallest Distance.

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<∞

Always check the previous graph when relaxing edges.

18
CSE 241: Algorithms
Bellman Ford
10
R2:
B
10

-8
A

5 C

Source 5
2

Always check the previous graph when relaxing edges.

19
CSE 241: Algorithms
Bellman Ford Negative edge Cycle
10
R3:
B
10

-8
A

5 C

Source 5
2

Check for Relaxation once again to check if negative cycle exists.

20
CSE 241: Algorithms
Bellman Ford Algorithm
Cycle Detection:

● After (V - 1) relaxations, a final check of all edges is performed.


● If any edge can still be relaxed, a negative cycle exists (an endlessly
decreasing path).

Complexity:

● Time Complexity: O(V * E), where V is the number of vertices and E is the
number of edges

21
CSE 241: Algorithms

You might also like