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

Chapter 3-Graph Algorithms: 2021 Prepared By: Beimnet G

This chapter discusses graph algorithms including methods for representing graphs and searching graphs. Graph searching involves systematically following graph edges to visit vertices. Minimum spanning trees and shortest paths are also explored, where edges have weights. Graph representations like adjacency lists and matrices are presented. Breadth-first search and depth-first search are two algorithms for determining connectivity between nodes in a graph.

Uploaded by

Betty Kebede
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Chapter 3-Graph Algorithms: 2021 Prepared By: Beimnet G

This chapter discusses graph algorithms including methods for representing graphs and searching graphs. Graph searching involves systematically following graph edges to visit vertices. Minimum spanning trees and shortest paths are also explored, where edges have weights. Graph representations like adjacency lists and matrices are presented. Breadth-first search and depth-first search are two algorithms for determining connectivity between nodes in a graph.

Uploaded by

Betty Kebede
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Chapter 3- Graph Algorithms

2021
Prepared by: Beimnet G.
Graph Algorithms
This chapter presents methods for representing a graph and for searching a
graph. Searching a graph means systematically following the edges of the
graph so as to visit the vertices of the graph.
Furthermore, the least-weight way of connecting all of the vertices together
when each edge has an associated weight (minimum-weight spanning tree) is
explored.
We will also consider how to compute shortest paths between vertices when
each edge has an associated length or “weight.”
2
Introduction
We begin by introducing some basic definitions surrounding graphs, and list a
spectrum of different algorithmic settings where graphs arise naturally.
A graph G is simply a way of encoding pairwise relationships among a set of
objects: it consists of a collection V of nodes (vertices) and a collection E of
edges, each of which ”joins” two of the nodes.
We thus represent an edge e ∈ E as a two-element subset of V : e = u, v for
some u, v ∈ V , where we call u and v the ends of e

3
Introduction
Edges in a graph indicate a symmetric relationship between their ends.
Often we want to encode asymmetric relationships, and for this we use the
closely related notion of a directed graph. A directed graph G’ consists of a set
of nodes V and a set of directed edges E’. Each e’ ∈ E’ is an ordered pair (u, v);
in other words, the roles of u and v are not interchangeable.
We call u the tail of the edge and v the head. We will also say that edge e’
leaves node u and enters node v.
A graph which is not directed is referred as an undirected graph. However, you
can generally assume a graph is undirected if it’s not specified otherwise.
4
Introduction
We define a path in an undirected graph G = (V, E) to be a sequence P of nodes
v1, v2, ..., vk−1, vk with the property that each consecutive pair v i , vi+1 is joined by
an edge in G.
P is often called a path from v1to vk, or a v1 − vk path.
A path is called simple if all its vertices are distinct from one another.
A cycle is a path v1, v2, ..., vk−1, vk in which k > 2,the first k-1 nodes are all
distinct, and v1 = vk

5
Introduction
We say that an undirected graph is connected if, for every pair of nodes u and
v, there is a path from u to v.
Choosing how to define connectivity of a directed graph is a bit more subtle,
since it’s possible for u to have a path to v while v has no path to u.
We say that a directed graph is strongly connected if, for every two nodes u
and v, there is a path from u to v and a path from v to u.

6
Introduction
Shortest Path is define as the distance between two nodes
u and v to be the minimum number of edges in a u − v path.
We can designate some symbol like ∞ to denote the
distance between nodes that are not connected by a path.
Trees- an undirected graph is a tree if it is connected and
does not contain a cycle. Trees are the simplest kind of
connected graph, deleting any edge from a tree will
disconnect it.
Tree concepts: root, parent, child, descendant, leaf.
7
Introduction

Both figures represent the same graph. The second picture is a tree
representation of the graph rooted at node 1.
8
Introduction
Tree Statements:
● Every n node tree has exactly n-1 edges
Each node other than the root has a single edge leading ”upward” to its parent;
and conversely, each edge leads upward from precisely one non-root node
● Let G be an undirected graph with n nodes. Any two of the following
statements implies the third.
1. G is connected.
2. G does not contain a cycle.
3. G has n − 1 edges.
9
Examples of Graphs
In some cases the nodes and edges both correspond to physical objects in the
real world, in others the nodes are real objects while the edges are virtual or
both could be abstractions.
● Transportation Networks
● Communication Networks
● Information Networks (WWW)
● Social Networks
● Dependency Networks- University course management, software system

10
Graph Representation
● 2 standard graph representations for any graph G = (V, E):
○ Adjacency lists-
provides a compact way to represent sparse graphs - those for which |E| is
much less than |V |2. It is usually the method of choice.
○ Adjacency matrix-
prefered when the graph is dense - |E| is close to |V |2- or when we need to
be able to tell quickly if there is an edge connecting two given vertices.

11
Graph Representation
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 such that
there is an edge (u,v) ∈ E. That is, Adj[u] consists of all the vertices adjacent to
u in G.
Since the adjacency lists represent the edges of a graph, in pseudocode we
treat the array Adj as an attribute of the graph, just as we treat the edge set E. In
pseudocode, therefore, we will see notation such as G.Adj[u].
12
Graph Representation
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.
The adjacency-list representation requires Θ(V+E) memory.
If there is a weight(cost) associated with an edge, the adjacency list
representation can be adjusted to accommodate that.
13
Graph Representation
A potential disadvantage of the adjacency-list representation is that it provides no
quicker way to determine whether a given edge (u,v) is present in the graph than to
search for v in the adjacency list Adj[u]. An adjacency-matrix representation of the
graph remedies this disadvantage, but at the cost of using asymptotically more
memory.
For the adjacency-matrix representation of a graph G= (V ,E ), we assume that the
vertices are numbered 1, 2, 3,... |V| in some arbitrary manner. Then the
adjacency-matrix representation of a graph G consists of a |V| x |V| matrix A=(aij) such
that
aij= 1 if (i,j) ∈ E,
0 otherwise
14
Graph Representation: Example
Represent this graph using an adjacency list .

15
Graph Representation: Example
Represent this graph using an adjacency matrix.

16
Graph Representation
The adjacency matrix of a graph requires Θ(V 2 ) memory, independent of the
number of edges in the graph.
Since in an undirected graph, (u, v) and (v, u) represent the same edge, the
adjacency matrix A of an undirected graph is its own transpose: A = A T
In some applications, it pays to store only the entries on and above the diagonal
of the adjacency matrix, thereby cutting the memory needed to store the graph
almost in half.
Like the adjacency-list representation of a graph, an adjacency matrix can be
modified to represent a weighted graph.
17
Graph Representation: Example
Represent this directed graph using an adjacency
list .

18
Graph Representation: Example
Represent this directed graph using an adjacency
matrix.

19
Representing Attributes
Most algorithms that operate on graphs need to maintain attributes for vertices
and/or edges.
We indicate these attributes as v.d for an attribute d of a vertex v.
When we indicate edges as pairs of vertices, we use the same style of notation.
For example, if edges have an attribute f , then we denote this attribute for edge
(u, v) by (u, v).f.

20
Graph Traversal
Having built up some fundamental notions regarding graphs, we turn to a very
basic algorithmic question: node-to-node connectivity.
Suppose we are given a graph G = (V, E) and two particular nodes s and t. We’d
like to find an efficient algorithm that answers the question: Is there a path from
s to t in G? We will call this the problem of determining s − t connectivity.
In this section, we describe two natural algorithms for this problem at a high
level: breadth-first search (BFS) and depth-first search (DFS).

21
Breadth First Search (BFS)
The simplest algorithm for determining s−t connectivity.
In BFS which we explore outward from s in all possible directions, adding nodes
one ”layer” at a time. Thus we start with s and include all nodes that are joined
by an edge to s - this is the first layer of the search. We then include all
additional nodes that are joined by an edge to any node in the first layer - this is
the second layer. We continue in this way until no new nodes are encountered.

Now to a more formal definition ….


22
Breadth First Search (BFS)
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 v reachable from s, the simple path in the breadth-first
tree from s to corresponds to a “shortest path” from s to v in G, that is, a path
containing the smallest number of edges.
23
Breadth First Search (BFS)
The algorithm discovers all vertices at distance k from s before discovering any vertices at
distance k+1.
A vertex is discovered the first time it is encountered during the search.
For each j ≥ 1, layer Lj produced by BFS consists of all nodes at distance exactly j from s. There
is a path from s to t if and only if t appears in some layer.
To keep track of progress, breadth-first search can color each vertex white, gray, or black. All
vertices start out white and may later become gray and then black.
Breadth-first search constructs a breadth-first tree, initially containing only its root (s). Whenever
the search discovers a white vertex v 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. We say that u is the
predecessor or parent of in the breadth-first tree.
Since a vertex is discovered at most once, it has at most one parent. 24
Breadth First Search (BFS)
The breadth-first-search procedure BFS here assumes
that the input graph G(V, E) is represented using
adjacency lists.
It attaches several additional attributes to each vertex in
the graph. We store the color of each vertex u ∈ V in
the attribute u.color and the predecessor (parent) of u in
the attribute u.π.
If u has no predecessor (for example, if u D s or u has
not been discovered), then u:π=NIL. The attribute u.d
holds the distance from the source s to vertex u
computed by the algorithm.
The algorithm also uses a first-in, first-out queue Q to
25
manage the set of gray vertices.
Breadth First Search (BFS)
Given the following graph, run the BFS procedure on it.

26
Breadth First Search (BFS): Connected
Component
The set of nodes discovered by the BFS algorithm is precisely those reachable
from the starting node s. We will refer to this set R as the connected component of
G containing s; and once we know the connected component containing s, we can
simply check whether t belongs to it so as to answer the question of s − t
connectivity.
BFS is just one possible way to produce this component. We can build the
component R by ”exploring” G in any order, starting from s.
To start off, we define R = {s}. Then at any point in time, if we find an edge (u, v)
where u ∈ R and v ∉ R, we can add v to R. If there is a path P from s to u, then there
is a path from s to v obtained by first following P and then following the edge (u, v).
The set R produced at the end of the algorithm is precisely the connected component of G 27
containing s
Breadth First Search (BFS): Shortest Path
Breadth-first search finds the distance to each reachable vertex in a graph G(V,E)
from a given source vertex s ∈ V .
We define the shortest-path distance as δ(s,v) from s to v as the minimum
number of edges in any path from vertex s to vertex v; if there is no path from s to
v then δ(s,v)= ∞
For a directed or undirected graph G=(V,E) and an arbitrary vertex s ∈ V: for any
edge (u,v) ∈ E, δ(s,v) <= δ(s,u)+1.
If vertices Vi and Vj are enqueued during the execution of BFS, and that V i is
enqueued before Vj . Then Vi.d <= Vj.d at the time that Vj is enqueued.
28
Breadth First Search (BFS)
Model Problems
1. Single_Pair_Reachability (G,s,t)
- Is there a path in G from s to t?
2. Single_Pair_Shortest_Path(G,s,t)
- What is the shortest distance from s to t in G? And what is the path?
3. Single_Source_Reachability(G,s)
- What is the distance from s to all vertices? And what is the path?

29
Breadth First Search (BFS)

A B C G

E F

30
Breadth First Tree
After you’ve determined the distance from s to a vertice, how do you store it?
And how do you store the path?
Solution: Breadth first tree (shortest path tree).
In a breadth first tree, you can construct the shortest path from each node to s by
recursively identifying the parent of each vertex.
Question:
● How are we sure it’s a tree?
● What happens if you add a path from A to D?
31
Depth First Search (DFS)
A recursive algorithm.
It searches “deeper” in the graph whenever possible and backtrack when it can’t
go any deeper.
Depth-first search explores edges out of the most recently discovered vertex that
still has unexplored edges leaving it until it has covered every vertex.
A depth first search may result in several trees.

32
Depth First Search (BFS)

A B C

D E F

33
Depth First Search (DFS): Algorithm

DFS_VISIT(u) DFS(G)
{ {

for each v in Adj[u]: for each u in G.V

if v.π ==NIL u.π = NIL

V.π = u for each u in G.V


if u.π ==NIL
DFS_VISIT(v)
DFS_VISIT(u)
}
}
34
Depth First Tree (DFS) Algorithm

35
Depth First Search (DFS)
Will not find the shortest path!
DFS has other applications.
DFS is useful in edge classification.
Every edge in a graph G gets visited at least once in a DFS (twice if it’s an
undirected graph)
What happens when you visit an edge?

36
Depth First Search (DFS): Edge Classification
● Tree Edge: a visit may lead to something unvisited resulting in a tree edge.
● Forward Edge: connects a node to its descendant
● Backward Edge: connects a node to its ancestor
● Cross Edge: any other node that connects edges that have no herricical
relation

Question: Which one of these edges do you think exist in an undirected graph?

37
Depth First Search (DFS): Edge Classification

Applications of Edge Classification:


● Cycle detection
● Topological Sort

38
Depth First Search (DFS): Cycle Detection
A graph G has a cycle ⇔ DFS (G) has a back edge.
Proof:
⇐ side proof: a back edge implies a cycle by definition.
⇒ side proof: there is a cycle in a path spanning vertices v1,v2,v3… vk if there
exists an edge (vk,v1). In a DFS edge (vk,v1) will be a back edge

39
Depth First Search (DFS): Topological Sort
Job Scheduling: given a directed acyclic graph (DAG) , order the vertices so
that all edges point from lower order to higher order vertices.
Topological Sort solves this problem
Topological sort uses DFS to order these nodes in a graph.
Algorithm:
Topological-Sort (G):
run DFS(G)
Output the reverse of finishing time (i.e when a node finishes add it
to the front of a list) 40
Depth First Search (DFS): Topological Sort
If there is an edge (u,v) then u appears before v in the ordering. That is, u must
be done before v can be done.
Example: This is how -
Professor Bumstead gets
dressed in the morning.

41
Topological Sort: Proof
Proof that topological sort gives a valid job scheduling solution.
For it to be correct:
for any edge (u,v), v finishes before u finishes.

Case 1: u starts before v


Case 2: v starts before u

42

You might also like