0% found this document useful (0 votes)
4 views

mod2

This document covers elementary graph algorithms, specifically focusing on graph representation methods such as adjacency lists and adjacency matrices, along with their memory requirements. It also details the breadth-first search (BFS) and depth-first search (DFS) algorithms, including their procedures, analyses, and properties. The document emphasizes the importance of these algorithms in exploring graph structures and their applications in various scenarios.

Uploaded by

joan.david.2k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

mod2

This document covers elementary graph algorithms, specifically focusing on graph representation methods such as adjacency lists and adjacency matrices, along with their memory requirements. It also details the breadth-first search (BFS) and depth-first search (DFS) algorithms, including their procedures, analyses, and properties. The document emphasizes the importance of these algorithms in exploring graph structures and their applications in various scenarios.

Uploaded by

joan.david.2k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

MODULE 2 - ELEMENTARY GRAPH ALGORITHMS

Faculty : Dr. Reshmi R

April 5, 2023

Faculty : Dr. Reshmi R April 5, 2023 1 / 58


Introduction - Representation of graphs

There are two standard ways to represent a graph G = (V, E):


1 as a collection of adjacency lists
2 as an adjacency matrix.
Either way is applicable to both directed and undirected graphs.
The adjacency-list representation is usually preferred, because it
provides a compact way to represent sparse graphs-those for which
|E | is much less than |V 2 |
An adjacency-matrix representation may be preferred, when the graph
is dense, i.e., |E | is close to |V 2 |.

Faculty : Dr. Reshmi R April 5, 2023 2 / 58


Representation of graphs- Adjacency List

The adjacency-list representation of a graph G = (V, E) consists of


an array Adj of |V | lists, one for each vertex in V.
For each (u,v), the adjacency list Adj[u] contains all the vertices v
such that there is an edge (u, v ) ∈ E . That is, Adj[u] consists of all
the vertices adjacent to u in G.
The vertices in each adjacency list are typically stored in an arbitrary
order.

Faculty : Dr. Reshmi R April 5, 2023 3 / 58


Representation of graphs- Adjacency List

If G is a directed graph, the sum of the lengths of all the adjacency


lists is |E |, since an edge of the form (u,v) is represented by having v
appear in Adj[u].
If G is an undirected graph, the sum of the lengths of all the
adjacency lists is 2|E |, since if (u,v) is an undirected edge, then u
appears in v’s adjacency list and vice versa.
For both directed and undirected graphs, for the adjacency-list
representation the amount of memory it requires is θ(V + E ).

Faculty : Dr. Reshmi R April 5, 2023 4 / 58


Representation of graphs- Adjacency Matrix

For the adjacency-matrix representation of a graph G = (V, E), we


assume that the vertices are numbered 1, 2, ..., |V | in some arbitrary
manner.
Then the adjacency-matrix representation of a graph G consists of a
|V | ∗ |V | matrix A = (aij ) such that

The adjacency matrix of a graph requires θ(V 2 ) memory, independent


of the number of edges in the graph.

Faculty : Dr. Reshmi R April 5, 2023 5 / 58


Representation of graphs - Undirected graph

Faculty : Dr. Reshmi R April 5, 2023 6 / 58


Representation of graphs - Directed graph

Faculty : Dr. Reshmi R April 5, 2023 7 / 58


Representation of graphs- Adjacency List

Adjacency lists can used to represent weighted graphs.


Let G = (V,E) be a weighted graph with weight function w. The
weight w(u,v) of the edge (u, v ) ∈ E is stored with vertex v in u’s
adjacency list.
The adjacency-list representation is robust in that it can be modified
to support many other graph variants.
A potential disadvantage of the adjacency-list representation is that
there is no quicker way to determine if a given edge (u, v) is present
in the graph than to search for v in the adjacency list Adj[u].

Faculty : Dr. Reshmi R April 5, 2023 8 / 58


Representation of graphs- Adjacency Matrix

Like the adjacency-list representation of a graph, the


adjacency-matrix representation can be used for weighted graphs.
If G = (V, E) is a weighted graph with edge-weight function w, the
weight w(u, v) of the edge (u, v ) ∈ E is simply stored as the entry in
row u and column v of the adjacency matrix.
If an edge does not exist, a NIL value can be stored as its
corresponding matrix entry, though for many problems it is convenient
to use a value such as 0 or ∞.

Faculty : Dr. Reshmi R April 5, 2023 9 / 58


Breadth First Search

Breadth-first search is one of the simplest algorithms for searching a


graph and the archetype for many important graph algorithms.
Given a graph G = (V, E) and a distinguished source vertex s,
breadth-first search systematically explores the edges of G to
”discover” every vertex that is reachable from s.
It computes the distance (smallest number of edges) from s to each
reachable vertex.
It also produces a ”breadth-first tree” with root s that contains all
reachable vertices.

Faculty : Dr. Reshmi R April 5, 2023 10 / 58


Breadth First Search

For any vertex v reachable from s, the path in the breadth-first tree
from s to v corresponds to a ”shortest path” from s to v in G, that is,
a path containing the smallest number of edges.
The algorithm discovers all vertices at distance k from s before
discovering any vertices at distance k + 1.
The algorithm works on both directed and undirected graphs.

Faculty : Dr. Reshmi R April 5, 2023 11 / 58


Breadth First Search - Procedure

Breadth-first search colors each vertex white, gray, or black.


All vertices start out white and may later become gray and then black.
A vertex is discovered the first time it is encountered during the
search, at which time it becomes nonwhite.
Gray and black vertices, therefore, have been discovered, but
breadth-first search distinguishes between them to ensure that the
search proceeds in a breadth-first manner.
If (u, v ) ∈ E and vertex u is black, then vertex v is either gray or
black; that is, all vertices adjacent to black vertices have been
discovered.
Gray vertices may have some adjacent white vertices; they represent
the frontier between discovered and undiscovered vertices.

Faculty : Dr. Reshmi R April 5, 2023 12 / 58


Breadth First Search - Procedure

Breadth-first search constructs a breadth-first tree, initially containing


only its root, which is the source vertex s.
Whenever a white vertex v is discovered in the course of scanning the
adjacency list of an already discovered vertex u, the vertex v and the
edge (u, v) are added to the tree. ’u’ is the predecessor or parent of v
in the breadth-first tree.
If u is on a path in the tree from the root s to vertex v, then u is an
ancestor of v and v is a descendant of u.

Faculty : Dr. Reshmi R April 5, 2023 13 / 58


Breadth First Search - Algorithm - parameters

The breadth-first-search procedure BFS assumes that the input graph


G = (V, E) is represented using adjacency lists.
It maintains several additional data structures with each vertex in the
graph.
The color of each vertex u ∈ V is stored in the variable color[u], and
the predecessor of u is stored in the variable π[u]. If u has no
predecessor (for example, if u = s or has not been discovered), then
π[u] = NIL.
The distance from the source s to vertex u computed by the
algorithm is stored in d[u].
The algorithm also uses a first-in, first-out queue Q to manage the set
of gray vertices.

Faculty : Dr. Reshmi R April 5, 2023 14 / 58


Breadth First Search - Algorithm

Faculty : Dr. Reshmi R April 5, 2023 15 / 58


Breadth First Search - Algorithm

Faculty : Dr. Reshmi R April 5, 2023 16 / 58


ENQUEUE - Algorithm

Faculty : Dr. Reshmi R April 5, 2023 17 / 58


DEQUEUE - Algorithm

Faculty : Dr. Reshmi R April 5, 2023 18 / 58


BFS - Example

Faculty : Dr. Reshmi R April 5, 2023 19 / 58


BFS - Example

Faculty : Dr. Reshmi R April 5, 2023 20 / 58


Breadth First Search - Analysis

After initialization, no vertex is ever whitened, and thus the test in


line 13 ensures that each vertex is enqueued at most once, and hence
dequeued at most once.
The operations of enqueuing and dequeuing take O(1) time, so the
total time devoted to queue operations is O(V ). Because the
adjacency list of each vertex is scanned only when the vertex is
dequeued, each adjacency list is scanned 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 ), and thus the total running
time of BFS is O(V + E ).
Thus, breadth-first search runs in time linear in the size of the
adjacency-list representation of G.

Faculty : Dr. Reshmi R April 5, 2023 21 / 58


DEPTH FIRST SEARCH

Faculty : Dr. Reshmi R April 5, 2023 22 / 58


Depth First Search

In depth-first search, edges are explored out of the most recently


discovered vertex v that still has unexplored edges leaving it.
When all of v’s edges have been explored, the search ”backtracks” to
explore edges leaving the vertex from which v was discovered.
This process continues until we have discovered all the vertices that
are reachable from the original source vertex.
If any undiscovered vertices remain, then one of them is selected as a
new source and the search is repeated from that source.
This entire process is repeated until all vertices are discovered.

Faculty : Dr. Reshmi R April 5, 2023 23 / 58


Depth First Search

Whenever a vertex v is discovered during a scan of the adjacency list


of an already discovered vertex u depth-first search records this event
by setting v’s predecessor field v .π to u.
A depth-first search may be composed of several trees, because the
search may be repeated from multiple sources. The predecessor
subgraph of a depth-first search is therefore defined slightly differently
from that of a breadth-first search: we let Gπ = (V , Eπ ), where
Eπ = {(v .π, v ) : v ∈ V and v .π �= NIL}
The predecessor subgraph of a depth-first search forms a depth-first
forest composed of several depth-first trees. The edges in Eπ are
called tree edges.

Faculty : Dr. Reshmi R April 5, 2023 24 / 58


Depth First Search-Procedure
Vertices are colored during the search to indicate their state.
Each vertex is initially white, is grayed when it is discovered in the
search, and is blackened when it is finished.
This technique guarantees that each vertex ends up in exactly one
depth-first tree, so that these trees are disjoint.
Besides creating a depth-first forest, depth-first search also
timestamps each vertex.
Each vertex v has two timestamps: the first timestamp v.d records
when v is first discovered (and grayed), and the second timestamp v.f
records when the search finishes examining v’s adjacency list (and
blackens v).
These timestamps are used in many graph algorithms and are
generally helpful in reasoning about the behavior of depth-first search.
These timestamps are integers between 1 and 2 ∗ |V |, since there is
one discovery event and one finishing event for each of the |V |
vertices.
For :every
Faculty vertex
Dr. Reshmi R u, u.d < u.f . April 5, 2023 25 / 58
DFS - Algorithm

Faculty : Dr. Reshmi R April 5, 2023 26 / 58


DFS - Algorithm

Faculty : Dr. Reshmi R April 5, 2023 27 / 58


DFS - Example

Faculty : Dr. Reshmi R April 5, 2023 28 / 58


DFS - Example

Faculty : Dr. Reshmi R April 5, 2023 29 / 58


DFS - Example

Faculty : Dr. Reshmi R April 5, 2023 30 / 58


DFS - Example

Faculty : Dr. Reshmi R April 5, 2023 31 / 58


Depth First Search-Explanation

Lines 1-3 paint all vertices white and initialize their π fields to NIL.
Line 4 resets the global time counter.
Lines 5-7 check each vertex in V in turn and when a white vertex is
found, visit it using DFS-VISIT.
Every time DFS-VISIT(u) is called in line 7, vertex u becomes the
root of a new tree in the depth-first forest.
When DFS returns, every vertex u has been assigned a discovery time
u.d and a finishing time u.f.

Faculty : Dr. Reshmi R April 5, 2023 32 / 58


Depth First Search-Explanation

In each call DFS-VISIT(G,u), vertex u is initially white.


Line 1 paints u gray, line 2 increments the global variable time, and
line 3 records the new value of time as the discovery time u.
Lines 4-7 examine each vertex v adjacent to u and recursively visit v if
it is white.
As each vertex v ∈ Adj[u] is considered in line 4, we say that edge (u,
v) is explored by the depth-first search.
Finally, after every edge leaving u has been explored, lines 8-9 paint u
black and record the finishing time in u.f.

Faculty : Dr. Reshmi R April 5, 2023 33 / 58


Depth First Search-Analysis

The results of depth-first search may depend upon the order in which
the vertices are examined in line 5 of DFS, and upon the order in
which the neighbors of a vertex are visited in line 4 of DFS-VISIT.
The loops on lines 1-3 and lines 5-7 of DFS take time θ(V ), exclusive
of the time to execute the calls to DFS-VISIT.
By aggregate analysis, the procedure DFS-VISIT is called exactly
once for each vertex v ∈ V , since DFS-VISIT is invoked only on white
vertices and the first thing it does is paint the vertex gray. During an
execution of DFS-VISIT(v), the loop on lines 4-7 is executed |Adj[v ]|
times.
Since the total cost of executing lines 4-7 of DFS-VISIT is θ(E ).
The running time of DFS is therefore θ(V + E ).

Faculty : Dr. Reshmi R April 5, 2023 34 / 58


Depth First Search-Properties

Depth-first search yields valuable information about the structure of a


graph.
Perhaps the most basic property of depth-first search is that the
predecessor subgraph Gπ does indeed form a forest of trees, since the
structure of the depth-first trees exactly mirrors the structure of
recursive calls of DFS-VISIT. That is, u = v .π if and only if
DFS-VISIT(v) was called during a search of u’s adjacency list.

Faculty : Dr. Reshmi R April 5, 2023 35 / 58


Depth First Search-Properties

Another important property of depth-first search is that discovery and


finishing times have parenthesis structure. If we represent the
discovery of vertex u with a left parenthesis ”(u” and represent its
finishing by a right parenthesis ”u)”, then the history of discoveries
and finishings makes a well-formed expression in the sense that the
parentheses are properly nested.

Faculty : Dr. Reshmi R April 5, 2023 36 / 58


Parenthesis theorem

In any depth-first search of a (directed or undirected) graph G = (V, E),


for any two vertices u and v, exactly one of the following three conditions
holds:
the intervals [u.d, u.f] and [v.d, v.f] are entirely disjoint, and neither u
nor v is a descendant of the other in the depth-first forest,
the interval [u.d, u.f] is contained entirely within the interval [v.d,
v.f], and u is a descendant of v in a depth-first tree, or
the interval [v.d, v.f] is contained entirely within the interval [u.d,
u.f], and v is a descendant of u in a depth-first tree.

Faculty : Dr. Reshmi R April 5, 2023 37 / 58


DFS - Properties of DFS

Faculty : Dr. Reshmi R April 5, 2023 38 / 58


DFS - Properties of DFS

Faculty : Dr. Reshmi R April 5, 2023 39 / 58


Depth First Search-Properties

Another interesting property of depth-first search is that the search


can be used to classify the edges of the input graph G = (V, E).
This edge classification can be used to glean important information
about a graph..

Faculty : Dr. Reshmi R April 5, 2023 40 / 58


Depth First Search-Properties

We can define four edge types in terms of the depth-first forest Gπ


produced by a depth-first search on G.
Tree edges are edges in the depth-first forestGπ . Edge (u, v) is a tree
edge if v was first discovered by exploring edge (u, v).
Back edges are those edges (u, v) connecting a vertex u to an
ancestor v in a depth first tree. Self-loops, which may occur in
directed graphs, are considered to be back edges.
Forward edges are those nontree edges (u, v) connecting a vertex u to
a descendant v in a depth-first tree.
Cross edges are all other edges. They can go between vertices in the
same depth-first tree, as long as one vertex is not an ancestor of the
other, or they can go between vertices in different depth-first trees.

Faculty : Dr. Reshmi R April 5, 2023 41 / 58


DFS - Example

Show how depth-first search works on the following graph. Assume that
the DFS procedure considers the vertices in alphabetical order, and assume
that each adjacency list is ordered alphabetically. Show the discovery and
finishing times for each vertex, and show the classification of each edge.
Also show the parenthesis structure of this depth-first search.

Faculty : Dr. Reshmi R April 5, 2023 42 / 58


DFS - Example

The tree edges are: (q, s), (s, v), (v, w), (q, t), (t, x), (x, z), (t, y), (r, u).
The back edges are: (w, s), (y, q), (z, x).
The forward edge is: (q, w).
The cross edges are: (u, y), (r, y).
The parentheses structure of the DFS of is (((())()))(()()).

Faculty : Dr. Reshmi R April 5, 2023 43 / 58


Topological Sort

Application of DFS
It is performed in directed acyclic graph(dag)
A topological sort of a graph can be viewed as an ordering of its
vertices along a horizontal line so that all directed edges go from left
to right.
Topological sorting is thus different from the usual kind of sorting.

Faculty : Dr. Reshmi R April 5, 2023 44 / 58


Topological Sort

A topological sort of a dag G = (V , E ) is a linear ordering of all its vertices


such that if G contains an edge (u, v), then u appears before v in the
ordering. (If the graph is not acyclic, then no linear ordering is possible.)

Faculty : Dr. Reshmi R April 5, 2023 45 / 58


Topological Sort-Example

a) Professor Bumstead topologically sorts his clothing when getting


dressed. Each directed edge (u, v) means that garment u must be put on
before garment v. The discovery and finishing times from a depth-first
search are shown next to each vertex.

Faculty : Dr. Reshmi R April 5, 2023 46 / 58


Topological Sort-Example

(b) The same graph shown topologically sorted. Its vertices are arranged
from left to right in order of decreasing finishing time. Note that all
directed edges go from left to right.

Faculty : Dr. Reshmi R April 5, 2023 47 / 58


Topological Sort-Algorithm

TOPOLOGICAL-SORT(G)

1 call DFS(G) to compute finishing times f[v] for each vertex v

2 as each vertex is finished, insert it onto the front of a linked list

3 return the linked list of vertices

Faculty : Dr. Reshmi R April 5, 2023 48 / 58


Topological Sort-Analysis

Topological sort can be performed in time θ(V + E ), since depth-first


search takes θ(V + E ) time and it takes O(1) time to insert each of the
|V | vertices onto the front of the linked list.

Faculty : Dr. Reshmi R April 5, 2023 49 / 58


Topological Sort-Example

Show the ordering of vertices produced by TOPOLOGICAL-SORT when it


is run on the following dag under the assumption that each adjacency list
is ordered alphabetically.

Faculty : Dr. Reshmi R April 5, 2023 50 / 58


Topological Sort-Example

Faculty : Dr. Reshmi R April 5, 2023 51 / 58


Strongly connected components

A classic application of depth-first search: decomposing a directed graph


into its strongly connected components.

Faculty : Dr. Reshmi R April 5, 2023 52 / 58


Strongly connected components

A digraph is said to be strongly connected if for any pair of two distinct


vertices u and v, there exists a directed path from u to v and a directed
path from v to u.
In general, a digraph’s vertiecs can be partitioned into disjoint maximal
subsets of vertices that are mutually accessible via directed path of
digraph, these subsets ae called strongly connected components.

Faculty : Dr. Reshmi R April 5, 2023 53 / 58


Strongly connected components-Example

A directed graph G. The strongly connected components of G are shown


as shaded regions. Each vertex is labeled with its discovery and finishing
times. Tree edges are shaded.

Faculty : Dr. Reshmi R April 5, 2023 54 / 58


Strongly connected components-Example

The graph G T , the transpose of G. The depth-first forest computed in line


3 of STRONGLY-CONNECTED-COMPONENTS is shown, with tree
edges shaded. Each strongly connected component corresponds to one
depth-first tree. Vertices b, c, g, and h, which are heavily shaded, are the
roots of the depth-first trees produced by the depth-first search of G T

Faculty : Dr. Reshmi R April 5, 2023 55 / 58


Strongly connected components-Example

The acyclic component graph G SCC obtained by contracting all edges


within each strongly connected component of G so that only a single
vertex remains in each component.

Faculty : Dr. Reshmi R April 5, 2023 56 / 58


Strongly connected components-Algorithm

STRONGLY-CONNECTED-COMPONENTS (G)

1 call DFS(G) to compute finishing times u.f for each vertex u

2 compute G T

3 call DFS(G T ), but in the main loop of DFS, consider the vertices in
order of decreasing u.f (as computed in line 1)

4 output the vertices of each tree in the depth-first forest formed in line 3
as a separate strongly connected component

Faculty : Dr. Reshmi R April 5, 2023 57 / 58


Strongly connected components-Idea

The idea behind this algorithm comes from a key property of the
component graph G SCC = (V SCC , E SCC ), which we define as follows.

Suppose that G has strongly connected components C1 , C2 , ..., Ck .


The vertex set V SCC is {v1 , v2 , ..., vk }, and it contains a vertex vi for each
strongly connected component Ci of G. There is an edge (vi , vj ) ∈ E SCC if
G contains a directed edge (x, y) for some x ∈ i and some y ∈ j.

Faculty : Dr. Reshmi R April 5, 2023 58 / 58

You might also like