Elementary Graph Algorithms: Hina Gul Assistant Professor Kinnaird College For Women, Lahore
Elementary Graph Algorithms: Hina Gul Assistant Professor Kinnaird College For Women, Lahore
Algorithms
Hina Gul
Assistant Professor
Kinnaird College for Women, Lahore
Introduction
• Graphs are important discrete structures because they are a flexible
mathematical model for many application problems.
• Any time there is a set of objects and there is some sort of
“connection” or “relationship” or “interaction” between pairs of
objects, a graph is a good way to model this.
• Examples of this can be found in computer and communication
networks transportation networks, e.g., roads, logic circuits surface
meshes for shape description in computer-aided design.
04/09/2021 2
Introduction (contd.)
• A graph G = (V, E) consists of a finite set of vertices V (or nodes) and E,
a binary relation on V called edges.
• E is a set of pairs from V.
• If a pair is ordered, we have a directed graph.
• For unordered pair, we have an undirected graph.
• A vertex w is adjacent to vertex v if there is an edge from v to w.
04/09/2021 3
Introduction (contd.)
• In a directed graph, the number of edges coming out of a vertex is
called the out-degree of that vertex.
• Number of edges coming in is the in-degree.
• In an undirected graph, we just talk of degree of a vertex. It is the
number of edges incident on the vertex.
04/09/2021 4
Introduction (contd.)
• For a digraph G = (V, E)
• where |E| means the cardinality of the set E, i.e., the number of edges. For an
undirected graph G = (V, E)
• A path in a directed graphs is a sequence of vertices (v0, v1, . . . , vk) such that
(vi−1, vi) is an edge for i = 1, 2, . . . , k.
• The length of the paths is the number of edges, k.
• A vertex w is reachable from vertex u is there is a path from u to w.
04/09/2021 5
Graph Representations
04/09/2021 6
Graph Representations
04/09/2021 7
Breadth-first search
• To motivate our first algorithm on graphs, consider the following problem.
We are given an undirected graph G = (V, E) and a source vertex s Ɛ V.
• The length of a path in a graph is the number of edges on the path. We
would like to find the shortest path from s to each other vertex in the
graph.
• Breadth-first search is one of the simplest algorithms for searching a
graph and the model for many important graph algorithms.
• Prim’s minimum-spanning tree algorithm (Section 23.2) and Dijkstra’s
single-source shortest-paths algorithm (Section 24.3) use ideas similar to
those in breadth-first search.
04/09/2021 8
Breadth-first search
• 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.
• For any vertex reachable from s, the simple path in the breadth-first
tree from s to corresponds to a “shortest path” from s to in G, that is,
a path containing the smallest number of edges.
• The algorithm works on both directed and undirected graphs.
04/09/2021 9
Breadth-first search
• Breadth-first search is so named because it expands the frontier
between discovered and undiscovered vertices uniformly across the
breadth of the frontier.
• That is, the algorithm discovers all vertices at distance k from s before
discovering any vertices at distance k + 1.
04/09/2021 10
Example (contd.)
• To keep track of progress, 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.
04/09/2021 11
Example
04/09/2021 12
Working Example
04/09/2021 13
Working Example (contd.)
04/09/2021 14
Working Example (contd.)
04/09/2021 15
Working Example (contd.)
04/09/2021 16
Working Example (contd.)
04/09/2021 17
Working Example
(contd.)
04/09/2021 18
Working Example (contd.)
04/09/2021 19
BFS Algorithm
04/09/2021 20
Analysis
• After initialization, breadth-first search never whitens a vertex, 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, and so the total time
devoted to queue operations is O(V ).
• Because 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 ) and thus the total running time of the BFS
procedure is O(V + E). Thus, breadth-first search runs in time linear in the size of the
adjacency-list representation of G.
04/09/2021 21
In Class Challenge
Starting from s, in what order will the nodes will be finished.
04/09/2021 22
In Class Challenge
• Starting from a, in what order will the nodes will be discovered.
04/09/2021 23
Home Work
• Find out a procedure (execution + time complexity) that prints out the
vertices on a shortest path from s to v ,assuming that BFS has already
computed a breadth-first tree
04/09/2021 24