Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Week 10 (Graphs and Trees)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 65

Graphs and Trees

Course Code: CSC 2211 Course Title: Algorithms

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: Week No: 10 Semester: Spring 2019-2020


Lecturer: Name & email
Lecture Outline

• Graph Basics
• Graph Searching
• Depth First Search
• Breadth First Search
• Topological Sort
Graphs

⚫ Graph – mathematical object consisting of a set of:


 V = nodes (vertices, points).
 E = edges (links, arcs) between pairs of nodes.
 Denoted by G = (V, E).
 Captures pair wise relationship between objects.
 Graph size parameters: n = |V|, m = |E|.
V = { 1, 2, 3, 4, 5, 6, 7, 8 }
E = { {1,2}, {1,3}, {2,3}, {2,4}, {2,5}, {3,5}, {3,7}, {3,8},
{4,5}, {5,6} }
n=8
m = 11
Graphs
Undirected Graph: A graph whose edges are unordered pairs of vertices. That is,
each edge connects two vertices where edge (u, v) = edge (v, u).

Directed Graph: A graph whose edges are ordered pairs of vertices. That is, each
edge can be followed from one vertex to another vertex where edge (u, v) goes
from vertex u to vertex v.

Acyclic Graph: A graph with no path that starts and ends at the same vertex.

1 2 1 2 1 2

3 4 3 4 3 4

Directed Undirected Acyclic


graph graph graph
Graph Example

A directed graph G= (V, E), where


V = {1, 2, 3, 4, 5, 6} and
E={ (1,2), (2,2), (2,4), (2,5), (4,1), (4,5), (5,4), (6,3) }.
The edge (2,2) is self loop.
Vertex 5 has in-degree 2 and out-degree 1.
Vertex 4 is adjacent to vertex 5; {1, 5} is adjacent to 4;
3 is not adjacent to any other vertex except 6.
Graph Example

An undirected graph G = (V, E), where


V = {1, 2, 3, 4, 5, 6} and
E = { {1,2}, {1,5}, {2,5}, {3,6}}.
The vertex 4 is isolated.
Vertex 1, 2, 5 has degree 2; vertex 3, 6 has degree 1; vertex 4 has degree 0.
Vertex 3 is adjacent to vertex 6 and vice versa; {1, 5} is adjacent to 2; 4 is not
adjacent to any other vertex.
Graph Introduction
Complete graph: When every vertex is strictly connected to each other. (The number of
edges in the graph is maximum).
Degree of a vertex v: The degree of vertex v in a graph G, written d (v ), is the number of
edges incident to v, except that each loop at v counts twice (in-degree and out-degree for
directed graphs)

A B

F C

E D
d(A)=3, d(B)=3,d(C)=2

Complete Graph d(D)=3, d(E)=3,d(F)=2


Graph Introduction
Dense graph: When the number of edges in the graph is close to
maximum. (adjacency matrix is used to store info for this)

Sparse graph: When number of edges in the graph is very few.


(adjacency list is used to store info for this)
Graph Introduction

Weighted graph: associates weights with either the edges or the vertices

DAG: Directed acyclic graphs

Connected: if every vertex of a graph can reach every other vertex, i.e., every
pair of vertices is connected by a path

Strongly connected: every 2 vertices are reachable from each other (in a
digraph)

Connected Component: equivalence classes of vertices under “is reachable


from” relation. Simply put, it is a subgraph in which any two vertices
are connected to each other by paths, and which is connected to no
additional vertices in the supergraph.
Forests, DAG, Components

Weighted Graph
Graph Applications

❖ State-space search in Artificial Intelligence


❖ Geographical information systems, electronic street directory
❖ Logistics and supply chain management
❖ Telecommunications network design
❖ Many more industry applications
❖ The graphic representation of world wide web (www)
❖ Resource allocation graph for processes that are active in the
system.
❖ The graphic representation of a map
❖ Scene graphs: The contents of a visual scene are also managed
by using graph data structure.
Applications—Communication Network

2
3
8
1 10

4
5
9
11

6
7

Vertex = city, edge = communication link.


Applications—Driving Distance/Time Map

2
4 3
8 8
1 6 10
2 4 5
4 4 3
5
9
11
5 6

6 7
7

Vertex = city, edge weight = driving distance/time.


Applications—Street Map

2
3
8
1 10

4
5
9
11

6
7

Some streets are one way.


Graph Representation

Adjacency matrix: represents a graph as n x n matrix A (here, n is


the number of nodes/ vertices):
A[i, j] = 1 if edge (i, j)  E (or weight of edge)
= 0 if edge (i, j)  E

Storage requirements: O(n2)


❖ Using adjacency matrix is more efficient to represent dense
graphs
❖ Especially if store just one bit/edge
❖ Undirected graph: only need half of matrix
Graph Representation

(a) (b)

a. An undirected graph G having five vertices and six edges.


b. The adjacency-matrix representation of G.
Graph Representation

(a) (b)

a. A directed graph G having five vertices and eight edges.


b. The adjacency-matrix representation of G.
Graph Representation

(a) (b)

a. A directed weighted graph G having five vertices and six edges.


b. The adjacency-matrix representation of G.
Graph Representation

Adjacency list: list of adjacent vertices. For each vertex v  V,


store a list of vertices adjacent to v
Storage requirements: O(n+e)
Using adjacency list is more efficient to represent sparse graphs

(a) (b)
a. A directed graph G having five vertices and six edges.
b. The adjacency-list representation of G.
Graph Representation

(a) (b)

a. An undirected graph G having five vertices and six edges.


b. The adjacency-list representation of G.
Graph Representation

(a) (b)

a. A directed weighted graph G having six vertices and seven edges.


b. The adjacency-list representation of G.
Graph Searching
 Given: a graph G = (V, E), directed or undirected
 Goal: methodically explore every vertex and edge
 Ultimately: build a tree on the graph
 Pick a vertex as the root
 Choose certain edges to produce a tree
 Note: might also build a forest if graph is not connected

 Breadth-first search
 Depth-first search
 Other variants: best-first, iterated deepening search, etc.
Depth-First Search (DFS)
 Explore “deeper” in the graph whenever possible
 Edges are explored out of the most recently discovered vertex v that still has
unexplored edges (LIFO)
 When all of v’s edges have been explored, backtrack to the vertex from which
v was discovered
 computes 2 timestamps: d[ ] (discovered) and f[ ] (finished)
 builds one or more depth-first tree(s) (depth-first forest)

 Algorithm colors each vertex


 WHITE: undiscovered
 GRAY: discovered, in process
 BLACK: finished, all adjacent vertices have been discovered
Depth-First Search: The Code
DFS(G) DFS_Visit(u)
{
{ color[u] = GREY;
for each vertex u V time = time+1;
color[u] = WHITE; d[u] = time; // compute d[]
time = 0; for each v adjacent to u
for each vertex u V if (color[v] == WHITE)
if (color[u] == WHITE) p[v]= u // build tree
DFS_Visit(u); DFS_Visit(v);
color[u] = BLACK;
}
time = time+1;
f[u] = time; // compute f[]
}
DFS Classification of Edges

 DFS can be used to classify edges of G:


1. Tree edges: edges in the depth-first forest.
2. Back edges: edges (u, v) connecting a vertex u to an
ancestor v in a depth-first tree.
3. Forward edges: non-tree edges (u, v) connecting a
vertex u to a descendant v in a depth-first tree.
4. Cross edges: all other edges.

 DFS yields valuable information about the


structure of a graph.
Operations of DFS
u v w
Undiscovered

Discovered,
On Process

Finished, all
adjacent vertices x y z
have been
discovered

Discover time/ Finish time


Operations of DFS
u v w
Undiscovered 1/

Discovered,
On Process

Finished, all
adjacent vertices x y z
have been
discovered

Discover time/ Finish time


Operations of DFS
u v w
Undiscovered 1/ 2/

Discovered,
On Process

Finished, all
adjacent vertices x y z
have been
Tree edge
discovered

Discover time/ Finish time


Operations of DFS
u v w
Undiscovered 1/ 2/

Discovered,
On Process
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered

Discover time/ Finish time


Operations of DFS
u v w
Undiscovered 1/ 2/

Discovered,
On Process
4/ 3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered

Discover time/ Finish time


Operations of DFS
u v w
Undiscovered 1/ 2/

Discovered,
On Process
4/ 3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Discover time/ Finish time


Operations of DFS
u v w
Undiscovered 1/ 2/

Discovered,
On Process
4/
4/5 3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Discover time/ Finish time


Operations of DFS
u v w
Undiscovered 1/ 2/

Discovered,
On Process
4/5
4/ 3/
3/6
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Discover time/ Finish time


Operations of DFS
u v w
Undiscovered 1/ 2/7
2/

Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Discover time/ Finish time


Operations of DFS
u v w
Undiscovered 1/
1/8 2/7
2/

Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
Operations of DFS
u v w
Undiscovered 1/8
1/ 2/7
2/

Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
Operations of DFS
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/

Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
Operations of DFS
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/

Discovered,
On Process
4/5
4/ 3/6
3/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
Cross Edge
Operations of DFS
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/

Discovered,
On Process
4/5
4/ 3/6
3/ 10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
Cross Edge
Operations of DFS
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/

Discovered,
On Process
4/5
4/ 3/6
3/ 10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
Cross Edge
Operations of DFS
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/

Discovered,
On Process
4/5
4/ 3/6
3/ 10/11
10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
Cross Edge
Operations of DFS
u v w
Undiscovered 1/8
1/ 2/7
2/ 9/12
9/

Discovered,
On Process
4/5
4/ 3/6
3/ 10/11
10/
Finished, all
adjacent vertices x y z
have been
Tree edge
discovered
Back Edge

Forward Edge
Discover time/ Finish time
Cross Edge
DFS Analysis

 Running time of DFS = O(n+e)


 DFS (excluding DFS_Visit) takes O(n) time
 DFS_Visit:
 DFS_Visit( v ) is called exactly once for each vertex v
 During DFS_Visit( v ), adjacency list of v is scanned
once
 sum of lengths of adjacency lists = O(e)

 This type of aggregate analysis is an informal


example of amortized analysis
Cycle Detection
 Theorem: An undirected graph is acyclic iff a DFS
yields no back edges
 Proof
 If acyclic, no back edges by definition (because a back
edge implies a cycle)
 If no back edges, acyclic
 No back edges implies only tree edges (Why?)
 Only tree edges implies we have a tree or a forest
 Which by definition is acyclic.

 Thus, can run DFS to find whether a graph has a


cycle.
 How would you modify the code to detect cycles?
Topological Sort

 Find a linear ordering of all vertices of the DAG such that if G


contains an edge (u, v), u appears before v in the ordering.

 In general, there may be many legal topological orders for a


given DAG.

 Idea:
1. Call DFS(G) to compute finishing time f[ ]
2. Insert vertices onto a linked list according to decreasing order of f[ ]

 How to modify DFS to perform Topological Sort in O(n+e) time?


Things Things to Wear Earlier
Socks ……….
Shoes Socks, Shorts, Pants
Sorts ………
Pants Shorts
Belts Pants, Shirt
Shirt ………..
Jacket Belt, Tie
Tie Shirt
Watch …………..

Watch → Shirt → Tie → Shorts →Pants → Belt → Jacket → Socks → Shoes

Shirt → Tie → Watch → Socks → Shorts → Pants → Belt →Jacket → Shoes


Breadth-First Search (BFS)
 Given source vertex s,
 systematically explore the breadth of the frontier to
 discover every vertex reachable from s
 computes the distance d[ ] from s to all reachable vertices
builds a breadth-first tree rooted at s

 Algorithm
 colors each vertex:
 WHITE : undiscovered
 GRAY: discovered, in process
 BLACK: finished, all adjacent vertices have been discovered
BFS (Intuition)
BFS: The Code
BFS(G, s) {
initialize vertices;
Q = {s};
while (Q not empty) {
u = Dequeue(Q);
for each v adjacent to u do {
if (color[v] == WHITE) {
color[v] = GRAY;
d[v] = d[u] + 1;// compute d[]
p[v] = u; // build BFS tree
Enqueue(Q, v);
}
}
color[u] = BLACK;
}
}
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Example
BFS Analysis
 initialize : O(n)

 Loop: Queue operations and Adjacency checks

 Queue operations
 each vertex is enqueued/dequeued at most once. Why?
 each operation takes O(1) time, hence O(n)

 Adjacency checks
 adjacency list of each vertex is scanned at most once
 sum of lengths of adjacency lists = O(e)

 Total run time of BFS = O(n+e)


Breadth-First Search: Properties

 What do we get the end of BFS?


1. d[v] = shortest-path distance from s to v, i.e.
minimum number of edges from s to v, or ∞ if v not
reachable from s
 Proof : refer CLRS

2. a breadth-first tree, in which path from root s to any


vertex v represent a shortest path
 Thus can use BFS to calculate shortest path from one vertex
to another in O(n+e) time, for unweighted graphs.
Books

1. Introduction to Algorithms, Third Edition, Thomas H. Cormen, Charle E. Leiserson,


Ronald L. Rivest, Clifford Stein (CLRS).
2. Fundamental of Computer Algorithms, Ellis Horowitz, Sartaj Sahni, Sanguthevar
Rajasekaran (HSR)
References

• http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/11-
Graph/dfs.html
• CLRS: 22.1, 22.2, 22.3, 22.4, 22.5
• HSR: 2.2, 2.5

You might also like