7/31/2019 07 Reduced
1/40
Sunday, May 6, 12
mailto:[email protected]:[email protected]7/31/2019 07 Reduced
2/40
Graphs
Introduction
definitions
representation
basic algorithms
Graph search algorithms
depth-first search
breadth-first search
Exercises
2
Sunday, May 6, 12
7/31/2019 07 Reduced
3/40
Graphs
In mathematics, a graph is an abstractrepresentation of a set of objects where somepairs of the objects are connected by links.
the objects are vertices (or nodes or points)
the links are edges
The edges may be directed (asymmetric) orundirected (symmetric).
directed edges are usually called arcs
undirected edges can be called lines
3
Sunday, May 6, 12
7/31/2019 07 Reduced
4/40
Graphs
Graphs are basic subject studied by graph
theory.
Examples: road networks
the web
social networks
precedence constraints
...
4
Sunday, May 6, 12
7/31/2019 07 Reduced
5/40
Definition of graph
A graph is an ordered pair G = (V,E)comprising a set Vof nodes and aset E of edges.
Vand E are usually taken to befinite.
the order of a graph is |V|, the number
of vertices.
the size of a graph is |E|, the number
of edges.
the degree of a vertex is the number of
edges that connect to it.
5
HN
HK
HU
TP
BK
Sunday, May 6, 12
7/31/2019 07 Reduced
6/40
Definition of graph
A graph is a weighted graph if a
number (weight) is assigned to
each edge.
Such weight might represent costs,lengths or capacities... depending
on the problem at hand.
Adjacency relation: if (u,v) E is
an edge of G = (V,E), the vertices u
and v are said to be adjacent to
one another.
6
HN
HK
HU
TP
BK
1.0
1.51.1
2.1 1.0
2.01.7
Sunday, May 6, 12
7/31/2019 07 Reduced
7/40
Question
Question 1: Consider an undirected graph that
has n vertices, no parallel edges and is
connected (that is, in one piece). What is the
minimum and maximum number of edges thatthe graph could have, respectively?
1. n-1 and n(n-1)/2
2. n-1 and n2
3. n and 2n
4. n and nn
7
Sunday, May 6, 12
7/31/2019 07 Reduced
8/40
Sparse versus dense graphs
Let n be number of vertices, m be
number of edges
In most applications, n < m
7/31/2019 07 Reduced
9/40
Representation
Different data structures for the representationof graphs are used in practice.
Most common data structures are: adjacency lists: vertices are stored as records or
objects, every vertex stores a list of adjacent vertices
adjacency matrix: a two-dimensional matrix in
which the rows represent source vertices and thecolumns represent destination vertices.
unordered edge sequences: a sequence of edges,each edge contains a pair of vertices.
9
Sunday, May 6, 12
7/31/2019 07 Reduced
10/40
Adjacency lists
10
HN
HK
HU
TP
BK
HK HN TP NULL
HN HK BK HU TP NULL
BK HN TP NULL
HU HN TP NULL
TP HK HN BK HU NULL
Sunday, May 6, 12
7/31/2019 07 Reduced
11/40
Adjacency lists
11
0
1
5
3
2
4
6
0 1
1 2 3 5
2 3
3 0 6
4 2
5 0 3 6
6 4
Vertices are commonly
indexed by integers.
Sunday, May 6, 12
7/31/2019 07 Reduced
12/40
Adjacency matrix
12
HN
HK
HU
TP
BK
HK HN BK HU TP
HK 0 1 0 0 1
HN 1 0 1 1 1
BK 0 1 0 0 1
HU 0 1 0 0 1
TP 1 1 1 1 0
Symmetricmatrix
Sunday, May 6, 12
7/31/2019 07 Reduced
13/40
Adjacency matrix
13
0
1
5
3
2
4
6
0 1 2 3 4 5 6
0 0 1 0 0 0 0 0
1 0 0 1 1 0 1 02 0 0 0 1 0 0 0
3 0 0 0 0 0 0 1
4 0 0 1 0 0 0 0
5 1 0 0 1 0 0 1
6 0 0 0 0 1 0 0
Asymmetric
matrix
Sunday, May 6, 12
7/31/2019 07 Reduced
14/40
Adjacency matrix
Represent G by a n*n matrix a where aij = 1 if
and only if G has an edge (i,j)
Variants: aij = number of (i,j) edges (if parallel edges)
aij = weight of (i,j) edge (if any)
aij = 1 if G has edge (i,j), aij=-1 if G has edge(j,i)
14
Sunday, May 6, 12
7/31/2019 07 Reduced
15/40
Adjacency matrix
Question 2: How much space does an
adjacency matrix require, as a function of the
number n of vertices and the number m of
edges?
1. (n)
2. (m)
3. (m+n)
4. (n2)
15
Sunday, May 6, 12
7/31/2019 07 Reduced
16/40
Unordered edge sequences
16
HN
HK
HU
TP
BK
0
1
5
3
2
4
6
(HK, HN)
(HN, BK)
(HN, HU)
(BK, TP)
(HU, TP)
(HK, TP) (0,1)(1,2)
(1,3)
(1,5)
(2,3)
(3,6)(4,2)
(5,0)
(5,3)
(5,6)
(6,4)
(u,v) (v,u)
(u,v)(v,u)
Sunday, May 6, 12
7/31/2019 07 Reduced
17/40
Choice of representations
Which representation is better?
The answer depends on graph density and
operations needed.
17
Sunday, May 6, 12
7/31/2019 07 Reduced
18/40
Graph algorithms
Graph algorithms are a significant field ofinterest within computer science.
Typical high-level operations associated withgraphs aregraph search (orgraph traversal):
finding a path between two nodes
finding the shortest path from one node toanother
finding special paths (Hamiltonian path, Eulerpath)
18
Sunday, May 6, 12
7/31/2019 07 Reduced
19/40
Graphs
Introduction
definitions
representation basic algorithms
Graph search algorithms
depth-first search
breadth-first search
Exercises
19
Sunday, May 6, 12
7/31/2019 07 Reduced
20/40
Graph search
Graph search refers to the problem ofvisiting all nodes in a graph in a particularmanner.
a tree is a special kind of graph
tree traversal is a special case of graph search
In general graph search, each node mayhave to be visited more than once.
in tree traversal, each node is visited exactly once
20
Sunday, May 6, 12
7/31/2019 07 Reduced
21/40
Graph search
Some motivations:
check if a network is connected -- can get to anywherefrom anywhere else
driving direction
formulate a plan (example, how to fill in a Sudokupuzzle)
nodes = a partially completed puzzle
arcs = filling in one new square
compute the pieces or components of a graph
clustering, structures of the web graph, etc.
21
Sunday, May 6, 12
7/31/2019 07 Reduced
22/40
Graph search
Goals:
1. find everything findable from a given vertex
2. dont explore anything twice Generic algorithm (given graph G, vertex s)
initially, s explored, all other vertices unexplored
while possible choose an edge (u,v) with u explored and v
unexplored, if none halt
mark v explored
22
Sunday, May 6, 12
7/31/2019 07 Reduced
23/40
Graph search
Claim: at end of the algorithm, v exploredif and only if G has a path from s to v.
Proof of this claim is given in the course Datastructure and algorithms
How to choose among the possibly many
frontier edges? depth-first search (using a stack -- recursion)
breadth-first search (using a queue)
23
Sunday, May 6, 12
7/31/2019 07 Reduced
24/40
Graph search
Depth-first search:
explore aggressively like a maze, backtrack only whennecessary
compute topological ordering of directed acyclic graphs
compute connected components in directed graphs
Breadth-first search:
explore nodes in layers can compute shortest path
can compute connected components of an undirectedgraph
24
Sunday, May 6, 12
7/31/2019 07 Reduced
25/40
Graphs
Introduction
definitions
representation basic algorithms
Graph search algorithms
depth-first search
breadth-first search
Exercises
25
Sunday, May 6, 12
7/31/2019 07 Reduced
26/40
Depth-first search
Depth-first search (DFS) is an algorithm for
searching/traversing a tree or a graph.
start at root node of a tree or at some node of a graph
explore as far as possible along each branch beforebacktracking
Formally, DFS progresses by expanding the first child
node of the search tree
go deeper and deeper until a goal node is found or until anode that has no children is found
then the search backtracks, returning to the most recentnode that has not been visited
26
Sunday, May 6, 12
7/31/2019 07 Reduced
27/40
Depth-first search
27
HN
HK
HU
TP
BK
HK HN BK HU TP
HK 0 1 0 0 1
HN 1 0 1 1 1
BK 0 1 0 0 1
HU 0 1 0 0 1
TP 1 1 1 1 0
dfs(HK): HK, HN, BK, TP, HU
Sunday, May 6, 12
7/31/2019 07 Reduced
28/40
Depth-first search
Suppose that a graph G = (V,E) is representedby an adjacency matrix of size n*n: (a[i,j])n*n.
Let visit(u) is a function which visits a node u. Let visited[1..n] be a boolean array which
marks whether a node u has been alreadyvisited.
visited[u] = 1 if u has been visited
visited[u] = 0 otherwise
Initially, visited[u] = 0, for all u = 1,...,n
28
Sunday, May 6, 12
7/31/2019 07 Reduced
29/40
Depth-first search
29
void dfs(int u) { int v; visit(u); visited[u] = 1; for (v = 1; v < n; v++) if (visited[v] == 0 && a[u][v] == 1) { dfs(v); }}
v has not been visited
there exists edge (u,v)
void visit(int u) {
printf("%3d", u);}
/* Depth-first search from node u */
recursively visit v
int main(int argc, char **argv) {
initialize(); dfs(0); return 0;}
Sunday, May 6, 12
7/31/2019 07 Reduced
30/40
Depth-first search
30
0
1 2
5 4
3
Question 3: What is the result of dfs(0)?
Sunday, May 6, 12
7/31/2019 07 Reduced
31/40
Depth-first search
Claim: at end of the algorithm, v
marked as visited if and only if there
exists a path from s to v in G. Reason: particular instantiation of
genetic search procedure.
31
Sunday, May 6, 12
7/31/2019 07 Reduced
32/40
Graphs
Introduction
definitions
representation basic algorithms
Graph search algorithms
depth-first search
breadth-first search
Exercises
32
Sunday, May 6, 12
7/31/2019 07 Reduced
33/40
Breadth-first search
In breadth-first search (BFS), the nodes
are visited in layers:
start at root node of a tree or at some node of agraph and visit its neighboring nodes
then for each of those neighbor nodes in turn,visit their neighbor nodes which were
unvisited, and so on
All neighbor nodes are added to a queue
(FIFO data structure)
33
Sunday, May 6, 12
7/31/2019 07 Reduced
34/40
Breadth-first search
BFS (graph G, start vertex s):
1. marks as visited
2. let Q be a queue, initialized with s
3. while Q is not empty:
remove the first node of Q, call it u
for each edge (u,v), if v is unvisited:
mark v as visited
add v to Q (at the end)
34
Sunday, May 6, 12
7/31/2019 07 Reduced
35/40
Breadth-first search
35
void bfs(int s) { int u, v; visit(s); visited[s] = 1; enqueue(Q,s);while (!isEmpty(Q)) { u = dequeue(Q); for (v = 1; v < n; v++) if (visited[v] == 0 && a[u][v] == 1) { visit(v); visited[v] = 1;
enqueue(Q,v); } }}
Sunday, May 6, 12
7/31/2019 07 Reduced
36/40
Breadth-first search
36
0
1 3
2
5
Question 4: What is the result of bfs(0)?
4
Sunday, May 6, 12
7/31/2019 07 Reduced
37/40
Application of BFS
BFS can be applied to compute shortest paths on agraph.
Goal: compute dist(u), the fewest number of edges
on a path from s to u.
Extra code:
init: dist(u) = 0 if u = s, dist(u) = +if u s when considering edge (u,v), if v is unvisited then setdist(v) = dist(u) + 1
At termination, dist(u) = i if and only if u is in ithlayer, that is the shortest path from s to u has i edges.
37
Sunday, May 6, 12
7/31/2019 07 Reduced
38/40
Graphs
Introduction
definitions
representation basic algorithms
Graph search algorithms
depth-first search
breadth-first search
Exercises
38
Sunday, May 6, 12
7/31/2019 07 Reduced
39/40
Exercises
Exercise 1: Implement and test BFS and
DFS algorithm on different graphs
use different graph representations
use different start vertices
Exercise 2: Application of BFS to
compute the shortest paths:
compute and print dist(u) for all vertices u
39
Sunday, May 6, 12
7/31/2019 07 Reduced
40/40
Exercises
Exercise 3: A connectedcomponent of a an undirectedgraph is a subgraph in which
any two vertices areconnected to each other bypaths, and which is connectedto no additional vertices.
write a function to compute theconnected components of agraph using either the BFS orDFS algorithms.
40
There are 3 connected
components in this graph
Top Related