Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Graph_notes_algorithm

The document provides an overview of graphs and graph algorithms, covering definitions, types of graphs, and various graph representations such as adjacency matrices. It details elementary graph algorithms, graph traversal methods, and algorithms for minimum spanning trees and shortest paths. Additionally, it explains basic terms related to graphs, including vertices, edges, and different graph structures like directed and undirected graphs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Graph_notes_algorithm

The document provides an overview of graphs and graph algorithms, covering definitions, types of graphs, and various graph representations such as adjacency matrices. It details elementary graph algorithms, graph traversal methods, and algorithms for minimum spanning trees and shortest paths. Additionally, it explains basic terms related to graphs, including vertices, edges, and different graph structures like directed and undirected graphs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 115

Graphs and graph algorithms

July 2024

CSE201
Outline
1 Introduction
2 Types of graph
3 Representation of a graph
Adjacency matrix
4 Elementary Graph Algorithms
5 Graph Traversal Algorithms
Breadth-first search
Depth-first search
6 Minimum Spanning Trees
Kruskal’s algorithm
Prim’s algorithms
7 Single-Source Shortest Paths
Bellman-Ford’s algorithm
Dijkstra’s algorithm
8 All-Pair Shortest Paths
Floyd-Warshall algorithm
(July 2024) Graphs and graph algorithms CSE201 2 / 115
Introduction

(July 2024) Graphs and graph algorithms CSE201 3 / 115


Definition

A graph G comprises two components:


1 A set V of elements called nodes (or points or vertices)
2 A set E of edges such that each edge e in E is identified with a unique
(unordered) pair [u, v ] of nodes in V , denoted by e = [u, v ].
Both V and E are sets.
A graph G is represented by a tuple (V , E ).
Example: V = {1, 2, 3, 4, 5, 6}, E = {(1, 4), (1, 6), (2, 6), (4, 5), (5, 6)}

(July 2024) Graphs and graph algorithms CSE201 4 / 115


Basic terms

Adjacent vertices: Two nodes are called adjacent if they are


connected through an edge.
Suppose e = [u, v ].
The nodes u and v are called the end-points of e.
u and v are said to be adjacent nodes or neighbours.
The total number of vertices in the graph G is called the order
(cardinality) of the graph G .
1 It is represented by |V |.
2 The cardinality or order of the graph in the example is 6.
The total number of edges in a graph G is called the size of the graph
G.
1 It is represented by |E |.
2 The size of the graph G in the example is 5.

(July 2024) Graphs and graph algorithms CSE201 5 / 115


Basic terms

Self loop
An edge with the same start and end points is called a self-loop i.e.
e = (vi , vj ) is a self-loop iff vi = vj .

Parallel or multiple edge


Two or more edges that have the same start and end vertices are called
multiple/parallel edges.

(July 2024) Graphs and graph algorithms CSE201 6 / 115


Basic terms

The degree of the Vertex


The degree of a node u, written deg (u), is the number of edges containing
u,
OR,
The total number of edges incident on a vertex is called the degree of that
vertex.
If deg (u) = 0 if u does not belong to any edge - then u is called an
isolated node.
When we count the degree of a vertex, then the self-loop is counted
two times.
In the example graph, the degree of node 6 is 3.
In the graph the vertex 3 is called the isolated vertex.

(July 2024) Graphs and graph algorithms CSE201 7 / 115


Basic terms

Pendant Vertex
A vertex with degree one is called a pendent vertex of the graph G .
OR
If there is only one edge associated with any vertex then the vertex is
called the pendant vertex.
The vertex 2 in the example graph is a pendant vertex.

(July 2024) Graphs and graph algorithms CSE201 8 / 115


Walk, trail and path

1 A walk in a graph is a sequence of consecutive vertices and edges


that begins and ends with a vertex.
The length of a walk is the number of edges in the walk.
2 Types of walks: There are several types of walks
Closed walk: A walk that begins and ends at the same vertex.
Open walk: A walk that starts and ends at different vertices.
3 Trail is a walk in which no edge is repeated, but vertices can be
repeated.
Open trail: The trail whose starting and ending vertex is different is
called an open trail.
Closed trail: The trail whose starting and ending vertex is the same is
called a closed trail.
4 Path: A path is a trail with distinct vertices.
An open path starts and ends on different vertices.
A closed path (cycle) starts and ends on the same vertex.

(July 2024) Graphs and graph algorithms CSE201 9 / 115


Walk, trail and path

Closed Walk: uavfyfvgyhwbvfyeu


Closed Trail: wcxdyfvgyhw
Closed Path: xcwbvaueydx
(July 2024) Graphs and graph algorithms CSE201 10 / 115
Path and cycle

Cycle: A cycle is a closed path with all distinct vertices except the
start and end vertices.
Circuit: A circuit is the sequence of edges of a cycle.

(July 2024) Graphs and graph algorithms CSE201 11 / 115


Properties of path

A path P of length n from a node u to a node v is defined as a


sequence of n + 1 nodes.
P = (v0 , v1 , v2 , . . . , vn ) such that u = v0 ; vi−1 is adjacent to vi for
i = 1, 2, . . . , n; and vn = v .
The path P is said to be closed if v0 = vn .
The path P is said to be simple if all the nodes are distinct, with the
exception that v0 may be equal to vn .
A cycle is a closed simple path with a length of 3 or more.
A cycle of length k is called a k-cycle.

(July 2024) Graphs and graph algorithms CSE201 12 / 115


Types of graph

(July 2024) Graphs and graph algorithms CSE201 13 / 115


Types of graph

1 Null graph
2 Regular graph
3 Undirected graph
4 Directed graph
5 Multigraph
6 Simple graph
7 Weighted graph
8 Unweighted graph
9 Complete graph
10 Connected graph

(July 2024) Graphs and graph algorithms CSE201 14 / 115


Null and simple graph
Null graph
For a given graph G , then vertex set may be written as V (G ) and the
edge as E (G ).
Null graph: A graph with order 0 and size 0.
For a null graph G , V (G ) = ϕ and E (G ) = ϕ. In short G = ϕ.

Simple graph
A graph is simple if it has no loops and no two of its edges join the
same pair of vertices.
In a simple graph with n vertices, every vertex degree is at most n − 1.

(July 2024) Graphs and graph algorithms CSE201 15 / 115


Regular graph

A graph G is k−regular if d(v ) = k, ∀v ∈ V (G )


A graph G which is k−regular for some value of k, is called a regular
graph.

(July 2024) Graphs and graph algorithms CSE201 16 / 115


Multigraph

A multigraph is an undirected graph in which multiple edges and


self-loops are allowed.
Such a generalization is called a multigraph.
The definition of a graph usually does not allow either multiple edges
or loops.

(July 2024) Graphs and graph algorithms CSE201 17 / 115


Directed graph
A directed graph (digraph) G = (V , E ) is a graph with a two
mappings
1 init : E → V
2 term : E → V
assigning to every edge e ∈ E an initial vertex init(e) and a
terminal vertex term(e), such that the edge e is directed from init(e)
to term(e).
The edge (x, y) is not identical to edge (y, x).

Consider the edges (0,1) and (3,2).


They are not identical to (1,0) and
(2,3) respectively.
For e=(0,1), init(e) = 0 and
term(e) = 1
For e=(3,2), init(e) = 3 and
term(e) = 2
(July 2024) Graphs and graph algorithms CSE201 18 / 115
Directed graph
A directed graph D is an orientation of an (undirected) graph G if
V (D) = V (G ) and E (D) = E (G ), and if {init(e), ter (e)} = {x, y }
for every edge e = xy .
An edge in a digraph is an ordered pair (u,v), where an edge is
directed from u ∈ V (G ) to v ∈ V (G ), instead of an unordered pair
[u, v ].
An edge e is also called an arc.
Indegree and outdegree
The outdegree of a node u in G , written outdeg (u), is the number
of edges initiating at u.
The indegree of u, written indeg (u), is the number of edges
terminating at u.
A node u is called a source if it has a positive outdegree but zero
indegree.
u is called a sink if it has a zero outdegree but a positive indegree.
(July 2024) Graphs and graph algorithms CSE201 19 / 115
Indegree and outdegree

Source vertex: A
Sink vertex: E
(July 2024) Graphs and graph algorithms CSE201 20 / 115
Directed graph

A directed graph G is said to be or strongly connected if for each pair


(u, v) ∈ G there is a directed path from u to v and there is also a
path from v to u.
G is said to be unilaterally connected if for any pair (u, v) ∈ G there
is a path from u to v or a path from v to u.

(July 2024) Graphs and graph algorithms CSE201 21 / 115


Undirected graph
An undirected graph is a graph in which edges have no orientation.
The edge (x, y) is identical to the edge (y, x), i.e., they are not
ordered pairs, unlike a digraph.
The maximum number of edges possible in an undirected simple
graph is n(n - 1)/2, where |V (G )| =n.
A directed graph D is an orientation of an undirected graph G if
V (D) = V (G ) and E (D) = E (G ), and if {init(e), term(e)} = {x, y }
for every edge e = xy .

Figure: (a) Undirected graph (b) Directed graph after imposing the orientation.
(July 2024) Graphs and graph algorithms CSE201 22 / 115
Directed Acyclic Graph (DAG)

A Directed Acyclic Graph (DAG) is a directed graph that contains no


cycles.

(July 2024) Graphs and graph algorithms CSE201 23 / 115


Unweighted graph

An unweighted graph does not have any value (weight) associated


with any edge in the graph.
Unless specified otherwise, all graphs are assumed to be unweighted
by default.

(July 2024) Graphs and graph algorithms CSE201 24 / 115


Weighted graph
A weighted graph associates a value (weight) with every edge in the
graph.
With each edge e ∈ E of G let there be associated a real number
w (e), called its weight.
In real-time applications, these weights are used to indicate cost or
lengths.
An unweighted graph is a weighted graph with all edge weights as 1.

(July 2024) Graphs and graph algorithms CSE201 25 / 115


Complete graph

A simple undirected graph in which an edge joins each pair of distinct


vertices is called a complete graph.

A complete graph with n nodes will have n(n − 1)/2 edges.

(July 2024) Graphs and graph algorithms CSE201 26 / 115


Cyclic graph

A simple graph containing at least one cycle in it is called a cyclic


graph.

(July 2024) Graphs and graph algorithms CSE201 27 / 115


Tree

A simple graph with no cycle is called a Tree.


A connected graph T without any cycles is called a tree graph, free
tree, or a tree.
This means, in particular, that there is a unique simple path P
between any two nodes u and v in T .
If T is a finite tree with m nodes, then T will have m − 1 edges.

(July 2024) Graphs and graph algorithms CSE201 28 / 115


Example 1

(July 2024) Graphs and graph algorithms CSE201 29 / 115


Example 1

(July 2024) Graphs and graph algorithms CSE201 30 / 115


Representation of a graph

(July 2024) Graphs and graph algorithms CSE201 31 / 115


Graph representations

1 Adjacency matrix
2 Adjacency list

(July 2024) Graphs and graph algorithms CSE201 32 / 115


Adjacency matrix

(July 2024) Graphs and graph algorithms CSE201 33 / 115


Adjacency Matrix

An adjacency matrix is a square matrix representing a finite graph.


A different ordering of the nodes may result in a different adjacency
matrix of the same graph.
The adjacency matrices of the same graph are equivalent in that they
can be obtained by altering the positions of the rows and the
corresponding columns.
Then the adjacency matrix A of an undirected graph G is a
symmetric matrix.

Definition
For a simple unweighted graph with vertex set V , the adjacency matrix is
a square |V | × |V | matrix A such that its element:
1 Aij = 1, when there is an edge from vertex i to vertex j, and
2 Aij = 0, when there is no edge

(July 2024) Graphs and graph algorithms CSE201 34 / 115


Adjacency Matrix Representation:

Figure: Undirected graph

v1 v2 v3 v4
v1 0 1 0 1
v2  1
 0 1 0 
v3  0 1 1 1 
v4 1 0 1 0
Symmetric matrix

(July 2024) Graphs and graph algorithms CSE201 35 / 115


Adjacency Matrix Representation:

Figure: Directed graph

v1 v2 v3 v4
v1 0 1 0 1
v2  0
 0 1 0 
v3  0 0 1 1 
v4 0 0 0 0
Asymmetric matrix

(July 2024) Graphs and graph algorithms CSE201 36 / 115


Path matrix

Observe that aij gives the number of paths of length 1 from node vi
to node vj .
If you take A2 = AAT , then aij2 gives the number of paths of length 2
from node vi to node vj .
...
If you take Ak = AAT , then aijk gives the number of paths of length k
from node vi to node vj .
Hence, B = A + A2 + A3 + · · · + Ar , gives the number of paths of
length geq1 and ≤ r from node vi to node vj .

(July 2024) Graphs and graph algorithms CSE201 37 / 115


Path matrix

Let G be a simple directed graph with m nodes, v1 , v2 , ..., vm .


If G has m nodes, such a simple path must have length m − 1 or less,
or a cycle of length m or less.
B = A + A2 + · · · + Am
The path matrix or reachability matrix of G is the m-square matrix
P = [pij ] defined as follows:
1 Pij = 1, if Bij is non-zero and there is a path from vi to vj
2 Pij = 0, Otherwise

(July 2024) Graphs and graph algorithms CSE201 38 / 115


Path matrix

(July 2024) Graphs and graph algorithms CSE201 39 / 115


Path matrix

A directed graph G is said to be strongly connected if, for any pair of


nodes u and v in G, there are both a path from u to v and a path
from v to u.
G is strongly connected if and only if the path matrix P of G has no
zero entries.

(July 2024) Graphs and graph algorithms CSE201 40 / 115


Linked representation

The linked list representation of the graph is more flexible than the
matrix representation.
In this representation each vertex of the graph G is represented by a
linked list structure.
Space complexity: O(|V | + |E |)

(July 2024) Graphs and graph algorithms CSE201 41 / 115


Linked List Representation: Undirected graph

(July 2024) Graphs and graph algorithms CSE201 42 / 115


Linked List Representation: Directed graph

(July 2024) Graphs and graph algorithms CSE201 43 / 115


Graph using adjacency list

(July 2024) Graphs and graph algorithms CSE201 44 / 115


Graph using adjacency list

(July 2024) Graphs and graph algorithms CSE201 45 / 115


Elementary Graph Algorithms

(July 2024) Graphs and graph algorithms CSE201 46 / 115


Elementary operations on graphs

Node insertion.
Edge insertion.
Edge deletion.
Node deletion.

(July 2024) Graphs and graph algorithms CSE201 47 / 115


Node insertion (Directed graph)

Insert a node F .

Same rule applies for undirected graphs.


(July 2024) Graphs and graph algorithms CSE201 48 / 115
Edge insertion (Directed graph)

Insert edges (F , A), (F , C ), (B, F ).

(July 2024) Graphs and graph algorithms CSE201 49 / 115


Edge deletion (Directed graph)

Delete edge (A, C ).

(July 2024) Graphs and graph algorithms CSE201 50 / 115


Node deletion (Directed graph)

Delete node D.

(July 2024) Graphs and graph algorithms CSE201 51 / 115


Edge insertion (Undirected graph)
Insert edges (F , A), (F , C ), (B, F ).

(July 2024) Graphs and graph algorithms CSE201 52 / 115


Edge deletion (Undirected graph)
Delete edge (C , D).

(July 2024) Graphs and graph algorithms CSE201 53 / 115


Node deletion (Undirected graph)
Delete node E .

(July 2024) Graphs and graph algorithms CSE201 54 / 115


Graph Traversal Algorithms

(July 2024) Graphs and graph algorithms CSE201 55 / 115


Traversing a graph

There are two standard ways that this is done.


1 Breadth-First search (BFS)
2 Depth-First search (DFS)
The breadth-first search uses a queue.
The depth-first search will use a stack.
During the execution of our algorithms, each node N of G will be in
one of three states, called the status of N, as follows:
1 STATUS = 1: (Ready state) The initial state of the node N. (Colour
code: White)
2 STATUS = 2: (Waiting state.) The node N is on the queue or stack,
waiting to be processed (Colour code: Grey).
3 STATUS = 3: (Processed state.) The node N has been processed
(Colour code: Black).
BFS always generates a tree.
DFS may generate a forest.

(July 2024) Graphs and graph algorithms CSE201 56 / 115


Breadth-first search

(July 2024) Graphs and graph algorithms CSE201 57 / 115


BFS

BFS is up to the following applications


Shortest Path and Minimum Spanning Tree for unweighted graph.
Peer to Peer Networks. In Peer to peer-to-peer networks like
Bit-Torrent, Breadth First Search is used to find all neighbor nodes.
Crawlers in Search Engines.
Social Networking Websites: In social networks, we can find people
within a given distance ’k’ from a person using Breadth First Search
till ’k’ levels.
GPS Navigation systems: Breadth First Search is used to find all
neighboring locations.
Broadcasting in Network: In networks, a broadcasted packet follows
Breadth First Search to reach all nodes.
Cycle detection in an undirected graph.

(July 2024) Graphs and graph algorithms CSE201 58 / 115


Basic idea

First examine the starting node A.


Then examine all the neighbors of the neighbors of A and so on.
We need to keep track of a node’s neighbours and guarantee that no
node is processed more than once.
The above algorithm will process only those nodes that are reachable
from the starting node A.
This is similar to the level order search of a tree.

(July 2024) Graphs and graph algorithms CSE201 59 / 115


BFS execution

(July 2024) Graphs and graph algorithms CSE201 60 / 115


BFS execution

(July 2024) Graphs and graph algorithms CSE201 61 / 115


Complexity analysis

In BFS, each vertex is enqueued at most once, and hence dequeued


at most once.
The operations of enqueuing and dequeuing take O(1) time, and so
the total time devoted to queue operations is O(V ).
As the procedure scans the adjacency list of each vertex only when
the vertex is dequeued, it scans each adjacency list at most once.
Since the sum of the lengths of all the adjacency lists is Θ(E ), the
total time spent in scanning adjacency lists is O(E ).
The overhead for initialization is O(V ).
The total running time of the BFS procedure is O(V + E ).

(July 2024) Graphs and graph algorithms CSE201 62 / 115


Depth-first search

(July 2024) Graphs and graph algorithms CSE201 63 / 115


Algorithm idea

The general idea behind a depth-first search beginning at a starting node


A is as follows.
First examine the starting node.
Then examine each node N along a path P which begins at A; i.e., we
process a neighbor of A, then a neighbor of a neighbor of A, and so
on.
After coming to a “dead end”, i.e., to the end of the path P, we
backtrack on P until we can continue along another, path P. And so
on.
Similar to the in-order traversal of a binary tree.

(July 2024) Graphs and graph algorithms CSE201 64 / 115


DFS execution

(July 2024) Graphs and graph algorithms CSE201 65 / 115


DFS pseudocode

(July 2024) Graphs and graph algorithms CSE201 66 / 115


Types of edges

DFS results in different types of edges based on the order in which


they are traversed.
1 Tree edge: Edge (u, v ) is a tree edge if it was first discovered by
exploring edge (u, v ).
2 Back edges: Edges (u, v ) connecting a vertex u to an ancestor v in a
depth-first tree.
3 Forward edge: Non-tree edge connecting a vertex u to its descendent
in a depth-first tree.
4 All other edges are called cross edges.
When a new edge (u, v ) is explored, then based on the colour of v ,
the edge is
1 tree edge if v is WHITE.
2 back edge if v is GREY.
3 forward or cross edge if v is BLACK.

(July 2024) Graphs and graph algorithms CSE201 67 / 115


DFS edges

(July 2024) Graphs and graph algorithms CSE201 68 / 115


Applications

Detecting cycle in a graph: A graph has a cycle if and only if we see a


back edge during DFS.
Path Finding: We can specialize the DFS algorithm to find a path
between two given vertices u and z.
1 Call DFS(G, u) with u as the start vertex.
2 Use a stack S to keep track of the path between the start vertex and
the current vertex.
3 As soon as destination vertex z is encountered, return the path as the
contents of the stack.
Topological Sorting
Finding Strongly Connected Components of a graph

(July 2024) Graphs and graph algorithms CSE201 69 / 115


Complexity analysis
The time complexity of DFS depends on the data structure used to
represent the graph.
If the graph is represented as an adjacency list:
1 For each node, we discover all its neighbors by traversing its adjacency
list just once in linear time.
2 For a directed graph, the sum of the sizes of the adjacency lists of all
the nodes is E. So, the time complexity in this case is O(V ) + O(E) =
O(V + E).
3 For an undirected graph, each edge appears twice.
4 The time complexity for this case is O(V) + O(2E) ∼ O(V + E).
If the graph is represented as an adjacency matrix
1 For each node, we will have to traverse an entire row of length V in the
matrix to discover all its outgoing edges.
2 Note that each row in an adjacency matrix corresponds to a node in
the graph, and that row stores information about edges emerging from
the node.
3 The time complexity of DFS in this case is O(V × V ) = O(V2 ).
(July 2024) Graphs and graph algorithms CSE201 70 / 115
Difference

(July 2024) Graphs and graph algorithms CSE201 71 / 115


Minimum Spanning Trees

(July 2024) Graphs and graph algorithms CSE201 72 / 115


Minimum Spanning Tree
Find a tree T in a connected graph that connects all of the vertices.
We call such a tree a spanning tree since it “spans” the graph G i.e.
covers all the vertices in the graph.
We call the problem of determining the spanning tree T with the
minimum total edge weight as the minimum-spanning-tree problem.
Two minimum spanning tree algorithms are Prim’s and Kruskal’s.
Light edge: The edge with the smallest weight among all the edges
satisfying a certain property.
Safe edge: If A is a subset of an MST before the addition of an edge
e and remains so after the addition of e to A, then e is called a safe
edge.

(July 2024) Graphs and graph algorithms CSE201 73 / 115


Minimum Spanning Tree

Let A be a subset of edges in an MST.

(July 2024) Graphs and graph algorithms CSE201 74 / 115


Kruskal’s algorithm

(July 2024) Graphs and graph algorithms CSE201 75 / 115


Kruskal’s Algorithm

In Kruskal’s algorithm, set A (⊂ E ) of edges is a forest whose vertices


are all the vertices of the graph G .
The safe edge e added to A is always the least-weight edge in the
graph that connects two distinct components i.e. e is the edge
1 with the least weight that has not been added to A. (Light edge
property)
2 does not form a cycle.
Let C1 and C2 denote the two trees that are connected by (u, v ).
In this algorithm, the components are maintained as set data
structures that are disjoint.

(July 2024) Graphs and graph algorithms CSE201 76 / 115


Kruskal’s Algorithm

Initially each vertex is a separate component maintained as separate


sets.
At each step, the safe edge is greedily chosen to connect two
components and merge them into one component, such that each set
contains the vertices in one tree of the current forest.
At termination, there is only one component with the MST.
The two ends of a safe edge must belong to two different components.
To combine trees, Kruskal’s algorithm calls the UNION procedure.

(July 2024) Graphs and graph algorithms CSE201 77 / 115


Kruskal’s Algorithm

The operation FIND − SET (u) returns a representative element from


the set that contains u.
We can determine whether two vertices u and v belong to the same
tree by testing whether FIND − SET (u) equals FIND − SET (v ).

(July 2024) Graphs and graph algorithms CSE201 78 / 115


Kruskal’s Algorithm

(July 2024) Graphs and graph algorithms CSE201 79 / 115


Kruskal’s Algorithm

(July 2024) Graphs and graph algorithms CSE201 80 / 115


Kruskal’s Algorithm

Worst case time complexity of Kruskals Algorithm is O(ElogV ) or


O(ElogE ).
The edges are maintained as min heap.
The next edge can be obtained in O(logE ) time if the graph has E
edges.
Kruskal’s Algorithm takes O(ElogE ) time.
The value of E can be at most O(V2 ).
So, O(logV ) and O(logE ) are same.

(July 2024) Graphs and graph algorithms CSE201 81 / 115


Prim’s algorithms

(July 2024) Graphs and graph algorithms CSE201 82 / 115


Prim’s Algorithm

Prim’s algorithm has the property that the edges in the set A always
form a single tree.
The growth starts from a single root node r (arbitrarily chosen) and
grows until it covers all the vertices in V .
Each step adds to the tree A a light edge that connects A to an
isolated vertex in GA = (V , A).
This rule greedily adds only edges that are safe for A.
When the algorithm terminates, the edges in set A form a minimum
spanning tree.
The key to implementing Prim’s algorithm efficiently is to make the
selection of the new edge to the tree formed by the edges in A.

(July 2024) Graphs and graph algorithms CSE201 83 / 115


Working

The inputs to the algorithm are the graph G and the starting (root)
vertex r .
During execution, all the vertices that are not in the tree, reside in
the min-priority queue Q with a key field key and a field parent π.
key (v ) is the minimum weight of an edge connecting v to the tree.
(key (v ) = ∞ if not connected).
π(v ) is the parent of v in the MST.
At each step, the vertex u with minimum key is extracted from Q,
and the corresponding edge is added to A.
Then the adjacency list of u is scanned, and if there is an edge from u
to any vertex v ∈ Q with where the weight of (u, v ) is less than
key (v ), then key (v ) = weight(u, v ) is updated. The presence of v in
Q ensures the “safeness” of the edge (u, v ).

(July 2024) Graphs and graph algorithms CSE201 84 / 115


Prim’s Algorithm - Example

(July 2024) Graphs and graph algorithms CSE201 85 / 115


Prim’s Algorithm

(July 2024) Graphs and graph algorithms CSE201 86 / 115


Complexity

Suppose the queue Q is implemented using a min-heap.


Constructing the heap takes O(V ) time.(Line 1-5)
The body of the while loop runs |V | times.
Each Extract-Min operation takes O(logV ) time.
Total time for all the calls to Extract-Min operations is O(VlogV )
time.
The for loop in Line 8-11 runs for total O(E ) time since there are
2|E | entries in the adjacency list and each key update in Line 11
involves a call to Deceease − Key call which takes O(logV ) time.
So, overall time complexity O(VlogV + ElogV ).

(July 2024) Graphs and graph algorithms CSE201 87 / 115


Single-Source Shortest Paths

(July 2024) Graphs and graph algorithms CSE201 88 / 115


Single Source Shortest Path

It is a shortest path problem where the shortest path from a given


source vertex to all other remaining vertices is computed.
Dijkstras Algorithm and Bellman-Ford Algorithm are two famous
algorithms for solving the single-source shortest path problem.

(July 2024) Graphs and graph algorithms CSE201 89 / 115


Problem definition

Problem
Given a graph G = (V , E ), we want to find the shortest path from a given
source vertex s ∈ V to each vertex v ∈ V .
The algorithm for the single-source problem can solve many other
problems, including the following variants.
Single-destination shortest-paths problem: Find the shortest path
to a given destination vertex t from each vertex v. By reversing the
direction of each edge in the graph, we can reduce this problem to a
single-source problem.
Single-pair shortest-path problem: Find the shortest path from u
to for given vertices u and v. If we solve the single-source problem
with source vertex u, we solve this problem also.
All-pairs shortest-paths problem: Find the shortest path from u to
for every pair of vertices u and v.
(July 2024) Graphs and graph algorithms CSE201 90 / 115
Problem description

In a shortest-paths problem, we are given a weighted, directed graph


G = (V , E ), with weight function w : E → R mapping edges to
real-valued weights.
The weight w (p) of path p =< v0 , v1 , , vk > is the sum of the
weights of its constituent edges.
w (p) = ki=1 w (vi−1 , vi )
P

We define the shortest-path weight

A shortest path from vertex u to vertex v is then defined as any path


p with weight w (p) = δ(u, v )

(July 2024) Graphs and graph algorithms CSE201 91 / 115


Two important procedures

(July 2024) Graphs and graph algorithms CSE201 92 / 115


Problem description

(July 2024) Graphs and graph algorithms CSE201 93 / 115


Bellman-Ford’s algorithm

(July 2024) Graphs and graph algorithms CSE201 94 / 115


Bellman-Ford algorithm

The Bellman-Ford algorithm is used to solve the shortest path


algorithm.
It works even when the graph has negative edges.
Given a weighted, directed graph G = (V , E ) with source s and
weight function w : E → R, then algorithm returns
1 a boolean value if it detects a negative-weight cycle in G indicating
that no solution exists.
2 the shortest paths from the source and their weights if there is no
negative weight cycle.

(July 2024) Graphs and graph algorithms CSE201 95 / 115


Algorithm outline

∀v (v ∈ V ) we maintain an estimate of the shortest path from the


source s in the field v .d and the parent π(v ) of v in the shortest path
tree.
The algorithm iteratively relaxes all the edges progressively decreasing
the estimate of the vertices until it finally achieves the shortest path
value δ(v ), v ∈ V , or, it detects a negative weighted cycle.
The algorithm returns TRUE if and only if the graph contains no
negative-weight cycles that are reachable from the source.

(July 2024) Graphs and graph algorithms CSE201 96 / 115


Algorithm pseudocode

(July 2024) Graphs and graph algorithms CSE201 97 / 115


Algorithm pseudocode

One edge sequence: (t, x), (t, y ), (t, z), (x, t), (y , x), (y , z), (z, x), (z, s), (s, t), (s, y )
Time Complexity of Bellman-Ford algorithm is O(VE ), in case the graph
is dense E ≈ V 2 , O(V 3 )
(July 2024) Graphs and graph algorithms CSE201 98 / 115
Dijkstra’s algorithm

(July 2024) Graphs and graph algorithms CSE201 99 / 115


Dijkstra’s algorithm

Dijkstra’s algorithm solves the single-source shortest-paths problem


on a weighted, directed graph G = (V , E ) for the case in which all
edge weights are non-negative.
Dijkstra’s algorithm maintains a set S of vertices whose final
shortest-path weights from the source s have already been determined.
The algorithm repeatedly selects the vertex u ∈ V − S with the
minimum shortest-path estimate, adds u to S, and relaxes all edges
moving out of u.
In a standard implementation, the minimum estimates of the shortest
paths are maintained in a min heap in the form of a priority queue,
keyed by their d values.

(July 2024) Graphs and graph algorithms CSE201 100 / 115


Pseudocode

(July 2024) Graphs and graph algorithms CSE201 101 / 115


Example

(July 2024) Graphs and graph algorithms CSE201 102 / 115


Complexity analysis

The initialization step needs O(V ) time.


The while loop runs for |V | number of times.
One EXTRACT − MIN(Q) takes O(logV ) time. So, the total time
taken is O(VlogV )
Line 7-8 runs for |E | times. One update in RELAX takes O(logV )
time. So, the total time taken is O(ElogV ).
Final time complexity is O((V + E )logV )
If the graph is dense such that E ≈ V 2 , then complexity is
O(V 2 logV ).

(July 2024) Graphs and graph algorithms CSE201 103 / 115


All-Pair Shortest Paths

(July 2024) Graphs and graph algorithms CSE201 104 / 115


All pair shortest path problem

It is a shortest path problem where the shortest path between every


pair of vertices is computed.

(July 2024) Graphs and graph algorithms CSE201 105 / 115


Floyd-Warshall algorithm

(July 2024) Graphs and graph algorithms CSE201 106 / 115


Floyd Warshall Algorithm

The algorithm maintains


1 a two-dimensional array (D) that contains distances between the nodes.
2 a two-dimensional array (Π) containing the nodes’ parents along the
current estimated shortest path.
First distances are calculated only using direct edges between the
nodes and after this, the algorithm reduces the distances using
intermediate nodes in paths.

(July 2024) Graphs and graph algorithms CSE201 107 / 115


Floyd Warshall Algorithm

The key idea of the algorithm is to partition the process of finding the
shortest path between any two vertices into several incremental
phases.
Let us number the vertices starting from 1 to n.
For k = {1, 2, . . . , n}, before the k th phase
1 D[i][j] contains the value of the shortest path from i to j
2 Π[i][j] contains the parent of the vertex j in the shortest path from i.
only through the vertices with numbers less than k.

(July 2024) Graphs and graph algorithms CSE201 108 / 115


Floyd-Warshall Algorithm

( k = 0, we can fill the matrix D[i][j] as follows


Initially, for
wij , if there exists an edge between i and j of weight wij
D[i][j] =
0, otherwise
We can calculate the distance matrix for the k th phase
dnew [i][j] = min(d[i][j], d[i][k] + d[k][j]), for the k th vertex.

(July 2024) Graphs and graph algorithms CSE201 109 / 115


Floyd Warshall Algorithm

The algorithm works as follows:

The time-complexity is O(n3 )


(July 2024) Graphs and graph algorithms CSE201 110 / 115
Floyd Warshall Algorithm

(July 2024) Graphs and graph algorithms CSE201 111 / 115


Floyd Warshall Algorithm

(July 2024) Graphs and graph algorithms CSE201 112 / 115


Floyd Warshall Algorithm

(July 2024) Graphs and graph algorithms CSE201 113 / 115


Floyd Warshall Algorithm

(July 2024) Graphs and graph algorithms CSE201 114 / 115


Floyd Warshall algorithm

The advantage of the Floyd-Warshall algorithm is that it is easy to


implement.
However, the algorithm can only be used when the graph is so small
that a cubic time complexity is fast enough.

(July 2024) Graphs and graph algorithms CSE201 115 / 115

You might also like