Professional Documents
Culture Documents
Simple Algorithms On Graphs: Vnu University of Science, Hanoi
Simple Algorithms On Graphs: Vnu University of Science, Hanoi
L Hng Phng
Department of Mathematics, Mechanics and Informatics
phuonglh@gmail.com
1
Sunday, May 6, 12
Graphs
Introduction
denitions representation basic algorithms
Exercises
2
Sunday, May 6, 12
Graphs
In mathematics, a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links.
the objects are vertices (or nodes or points) the links are edges
Graphs
Graphs are basic subject studied by graph theory. Examples:
road networks the web social networks precedence constraints ...
4
Sunday, May 6, 12
Denition of graph
A graph is an ordered pair G = (V,E) comprising a set V of nodes and a set E of edges. V and E are usually taken to be nite.
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
Sunday, May 6, 12
HK
HN
BK
HU
TP
Denition 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
Sunday, May 6, 12
1.0
HK
HN
1.5
1.1 1.7
HU
2.0
BK
2.1
1.0
TP
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 that the 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
8
Sunday, May 6, 12
Representation
Different data structures for the representation of 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 the columns represent destination vertices. unordered edge sequences: a sequence of edges, each edge contains a pair of vertices.
9
Sunday, May 6, 12
Adjacency lists
HK HK HN TP
NULL
HN HN
HK
BK
HU
TP
NULL
BK
HN
TP
NULL
HU
HN
TP
NULL
BK
HU
TP
HK
HN
BK
HU
NULL
TP
10
Sunday, May 6, 12
Adjacency lists
2 1 3 6
0
4
1 2 3 0 2 0 4 3 6 6 3 5
1 2 3 4 5 6
11
Sunday, May 6, 12
Adjacency matrix
HK
HK HN BK HU TP HK
0 1 0 0 1
1 0 1 1 1
0 1 0 0 1
0 1 0 0 1
1 1 1 1 0
HN
HN BK HU
BK
HU
TP
TP
Symmetric matrix
12
Sunday, May 6, 12
Adjacency matrix
2 1 3 6
0
4
1 1 0 0 0 0 0 0
2 0 1 0 0 1 0 0
3 0 1 1 0 0 1 0
4 0 0 0 0 0 0 1
5 0 1 0 0 0 0 0
6 0 0 0 1 0 1 0
0 1 2 3 4 5 6
0 0 0 0 0 1 0
Asymmetric matrix
13
Sunday, May 6, 12
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
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
HN
(HK, HN) (HN, BK) (HN, HU) (BK, TP) (HU, TP) (HK, TP)
2 1 3 6
BK
HU
(0,1) (1,2) (1,3) (1,5) (2,3) (3,6) (4,2) (5,0) (5,3) (5,6) (6,4)
16
Sunday, May 6, 12
Choice of representations
Which representation is better?
The answer depends on graph density and operations needed.
17
Sunday, May 6, 12
Graph algorithms
Graph algorithms are a signicant eld of interest within computer science. Typical high-level operations associated with graphs are graph search (or graph traversal):
nding a path between two nodes nding the shortest path from one node to another nding special paths (Hamiltonian path, Euler path)
18
Sunday, May 6, 12
Graphs
Introduction
denitions representation basic algorithms
Exercises
19
Sunday, May 6, 12
Graph search
Graph search refers to the problem of visiting all nodes in a graph in a particular manner.
a tree is a special kind of graph tree traversal is a special case of graph search
In general graph search, each node may have to be visited more than once.
in tree traversal, each node is visited exactly once
20
Sunday, May 6, 12
Graph search
Some motivations:
check if a network is connected -- can get to anywhere from anywhere else driving direction formulate a plan (example, how to ll in a Sudoku puzzle)
nodes = a partially completed puzzle arcs = lling in one new square
Graph search
Goals:
1. nd everything ndable from a given vertex 2. dont explore anything twice
Graph search
Claim: at end of the algorithm, v explored if and only if G has a path from s to v.
Proof of this claim is given in the course Data structure and algorithms
Graph search
Depth-rst search:
explore aggressively like a maze, backtrack only when necessary compute topological ordering of directed acyclic graphs compute connected components in directed graphs
Breadth-rst search:
explore nodes in layers can compute shortest path can compute connected components of an undirected graph
24
Sunday, May 6, 12
Graphs
Introduction
denitions representation basic algorithms
Exercises
25
Sunday, May 6, 12
Depth-rst search
Depth-rst 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 before backtracking
Formally, DFS progresses by expanding the rst child node of the search tree
go deeper and deeper until a goal node is found or until a node that has no children is found then the search backtracks, returning to the most recent node that has not been visited
26
Sunday, May 6, 12
Depth-rst search
HK
HK HN BK HU TP HK
0 1 0 0 1
1 0 1 1 1
0 1 0 0 1
0 1 0 0 1
1 1 1 1 0
HN
HN BK HU
BK
HU
TP
TP
27
Sunday, May 6, 12
Depth-rst search
Suppose that a graph G = (V,E) is represented by 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 already visited.
visited[u] = 1 if u has been visited visited[u] = 0 otherwise
Depth-rst search
/* Depth-first search from node u */ void dfs(int u) { v has not been visited int v; visit(u); visited[u] = 1; for (v = 1; v < n; v++) if (visited[v] == 0 && a[u][v] == 1) { dfs(v); } there exists edge (u,v) } void visit(int u) { recursively visit v printf("%3d", u); }
int main(int argc, char **argv) { initialize(); dfs(0); return 0; } 29
Sunday, May 6, 12
Depth-rst search
1 0 5 4 2 3
30
Sunday, May 6, 12
Depth-rst 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
Graphs
Introduction
denitions representation basic algorithms
Exercises
32
Sunday, May 6, 12
Breadth-rst search
In breadth-rst search (BFS), the nodes are visited in layers:
start at root node of a tree or at some node of a graph and visit its neighboring nodes then for each of those neighbor nodes in turn, visit their neighbor nodes which were unvisited, and so on
Breadth-rst search
BFS (graph G, start vertex s):
1. mark s as visited 2. let Q be a queue, initialized with s 3. while Q is not empty:
remove the rst 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
Breadth-rst search
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); } } }
35
Sunday, May 6, 12
Breadth-rst search
1 3 5
36
Sunday, May 6, 12
Application of BFS
BFS can be applied to compute shortest paths on a graph. 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 set dist(v) = dist(u) + 1
At termination, dist(u) = i if and only if u is in ith layer, that is the shortest path from s to u has i edges.
37
Sunday, May 6, 12
Graphs
Introduction
denitions representation basic algorithms
Exercises
38
Sunday, May 6, 12
Exercises
Exercise 1: Implement and test BFS and DFS algorithm on different graphs
use different graph representations use different start vertices
Exercises
Exercise 3: A connected component of a an undirected graph is a subgraph in which any two vertices are connected to each other by paths, and which is connected to no additional vertices.
write a function to compute the connected components of a graph using either the BFS or DFS algorithms.
40
Sunday, May 6, 12