Graph_notes_algorithm
Graph_notes_algorithm
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
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 .
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.
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.
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
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.
Source vertex: A
Sink vertex: E
(July 2024) Graphs and graph algorithms CSE201 20 / 115
Directed graph
Figure: (a) Undirected graph (b) Directed graph after imposing the orientation.
(July 2024) Graphs and graph algorithms CSE201 22 / 115
Directed Acyclic Graph (DAG)
1 Adjacency matrix
2 Adjacency list
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
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
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
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 .
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 |)
Node insertion.
Edge insertion.
Edge deletion.
Node deletion.
Insert a node F .
Delete node D.
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.
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 ).
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
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
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.