0% found this document useful (0 votes)
3 views20 pages

Core Concepts: G (V, E) V E

The document explains core concepts of graph theory, including definitions of graphs, weighted graphs, paths, and shortest paths, as well as algorithms like Dijkstra's and Bellman-Ford for finding shortest paths. Dijkstra's algorithm utilizes a priority queue and relaxation technique to determine the shortest path from a source node, while Bellman-Ford can handle negative edge weights and checks for negative cycles. The time complexities for these algorithms are also discussed, with Dijkstra's being O(V^2) and Bellman-Ford O(V * E).

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)
3 views20 pages

Core Concepts: G (V, E) V E

The document explains core concepts of graph theory, including definitions of graphs, weighted graphs, paths, and shortest paths, as well as algorithms like Dijkstra's and Bellman-Ford for finding shortest paths. Dijkstra's algorithm utilizes a priority queue and relaxation technique to determine the shortest path from a source node, while Bellman-Ford can handle negative edge weights and checks for negative cycles. The time complexities for these algorithms are also discussed, with Dijkstra's being O(V^2) and Bellman-Ford O(V * E).

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/ 20

Core Concepts

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

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

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

Choose the one with the smallest Distance.

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

Always check the previous graph when relaxing edges.

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

-8
A

5 C

Source 5
2

Always check the previous graph when relaxing edges.

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

19
CSE 241: Algorithms
Dijkstra 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

20
CSE 241: Algorithms

You might also like