슬라이드 1 - Pusan

Download Report

Transcript 슬라이드 1 - Pusan

Graphs
2008, Fall
Pusan National University
Ki-Joune Li
PNU
STEM
Graph

Definition


G = (V,E )
where V : a set of vertices and
E = { <u ,v > | u ,v  V } : a set of edges
Some Properties


Equivalence of Graphs
Tree


a Graph
Minimal Graph
b
a
c
a
e
d
d
c
d
e
2
PNU
STEM
Some Terms

Directed Graph



Complete Graph



G is a directed graph iff <u,v >  <u,v > for u  v  V
Otherwise G is an undirected graph
Suppose nv = number(V )
Undirected graph G is a complete graph if ne = nv (nv - 1) / 2,
where ne is the number of edges
Adjacency


For a graph G=(V,E )
u is adjacent to v iff  e = <u,v > E , where u,v  V
If G is undirected, v is adjacent to u
otherwise v is adjacent from u
3
PNU
STEM
Some Terms

Subgraph


Path from u to v



G’ is a subgraph of a graph G iff
V(G’)  V(G) and E(G’)  E(G)
A sequence of vertices u=v0, v1, v2, … vn=v  V , such that
<vi ,vi +1>  E for every vi
Cycle iff u = v
Connected



Two vertices u and v are connected iff  a path from u to v
Graph G is connected iff for every pair u and v there is a path
from u to v
Connected Components

A connected subgraph of G
4
PNU
STEM
Some Terms

Connected



An directed graph G is strongly connected iff
iff for every pair u and v there is a path from u to v and
path from v to u
DAG: directed acyclic graph (DAG)
In-degree and Out-Degree


In-degree of v : number of edges coming to v
Out-degree of v : number of edges going from v
5
PNU
STEM
Representation of Graphs: Matrix

Adjacency Matrix


A[ i, j ] = 1, if there is an edge <vi ,vj >
A[ i, j ] = 0, otherwise
0
1
Example
0 1 1 1
0 0 1 0
2
3
0 0 0 1
0 0 0 0

Undirected Graph: Symmetric Matrix
 Space complexity: O(nv2) bits
 In-degree (out-degree) of a node
6
PNU
STEM
Representation of Graphs: List

Adjacency List

Each node has a list of adjacent nodes
0
1
1
2
2
3
2
3
0
3


Space Complexity: O(nv + ne )
Inverse Adjacent List
1
2
3
0
1
0
2
0
1
3
0
2
7
PNU
STEM
Weighted Graph: Network

For each edge <vi ,vj >, a weight is given


Example: Road Network
Adjacency Matrix


A[ i, j ] = wij, if there is an edge <vi ,vj > and wij is the weight
A[ i, j ] = , otherwise

1.5
2.3
1.2


1.0




1.9




Adjacency List
0
1 1.5
1
2 1.0
2
3 1.9
3
1.5
1
1.0
0
2.3
1.2
2
1.9
2 2.3
3
3 1.2
8
PNU
STEM
Graph: Basic Operations

Traversal




Depth First Search (DFS)
Breadth First Search (BFS)
Used for search
Example

Find Yellow Node from 0
0
1
4
2
3
5
7
6
9
PNU
STEM
DFS: Depth First Search
void Graph::DFS() {
visited=new Boolean[n];
for(i=0;i<n;i++)visited=FALSE;
DFS(0);
delete[] visited;
}
0
1
4
2
void Graph::DFS(int v) {
visited[v]=TRUE;
for each u adjacent to v {
if(visited[u]==FALSE) DFS(w);
}
}
3
5
7
6
Adjacency List: Time Complexity: O(e)
Adjacency Matrix: Time Complexity: O(n2)
10
PNU
STEM
BFS: Breadth First Search
void Graph::BFS() {
visited=new Boolean[n];
for(i=0;i<n;i++)visited=FALSE;
Queue *queue=new Queue;
queue.insert(v);
while(queue.empty()!=TRUE) {
v=queue.delete()
for every w adjacent to v) {
if(visited[w]==FALSE) {
queue.insert(w);
visited[w]=TRUE;
}
}
}
delete[] visited;
}
0
1
4
2
3
5
7
6
Adjacency List: Time Complexity: O(e)
Adjacency Matrix: Time Complexity: O(n2)
11
PNU
STEM
Spanning Tree

A subgraph T of G = (V,E ) is a spanning tree of G

iff T is tree and V (T )=V (G )
0
1
4

Finding Spanning Tree: Traversal


2
DFS Spanning Tree
BFS Spanning Tree
3
5
6
7
0
0
1
1
4
4
2
2
3
5
7
6
3
5
7
6
12
PNU
STEM
Articulation Point and Biconnected
Components

Articulation Point

A vertex v of a graph G is an articulation point iff
the deletion of v makes G two connected components
0
0
1
4
1
7
2
5
1
6
3
4
7
2
5

6
6
3
Biconnected Graph


2
Connected Graph without articulation point
Biconnected Components of a graph
13
PNU
STEM
Finding Articulation Point: DFS Tree

Back Edge and Cross Edge of DFS Spanning Tree



Tree edge: edge of tree
Back edge: edge to an ancestor
Cross edge: neither tree edge nor back edge

No cross edge for DFS spanning tree
2
0
Back edge
1
1
7
0
4
6
2
4
6
3
5
3
5
Back edge
7
14
PNU
STEM
Finding Articulation Point

Articulation point


Root is an articulation point if it has at least two children.
u is an articulation point
if  child of u without back edge to an ancestor of u
2
Back edge
1
0
6
4
3
5
Back edge
7
15
PNU
STEM
Finding Articulation Point by DFS Number

DFS number of a vertex: dfn(v)

0
The visit number by DFS
1


1
0
low(v)= min{
2 0
dfn(v),
min{ low (x)| x: child of v },
min{ dfn(x)| dfn(x)| (v, x): back edge } }
6
5
Low number: low(v)

0
2
3
2
4
3
0
4
5
5
5
6
0
5
7
7
v is an articulation Point


If v is the root node with at least two children or
If v has a child u such that low(u)  dfn(v)

v is the boss of subtree: if v dies, the subtree looses the line
16
PNU
STEM
Computation of dfs(v) and low(v)
void Graph::DfnLow(x) {
num=1; // num: class variable
for(i=0;i<n;i++) dfn[i]=low[i]=0;
DfnLow(x,-1);//x is the root node
}
void Graph::DfnLow(int u,v) {
dfn[u]=low[u]=num++;
for(each vertex w adjacent from u){
if(dfn[w]==0) // unvisited w
DfnLow(w,u);
low[w]=min(low[u],low[w]);
else if(w!=v)
low[u]=min(low[u],dfn[w]); //back edge
}
}
Adjacency List: Time Complexity: O(e)
Adjacency Matrix: Time Complexity: O(n2)
17
PNU
STEM
Minimum Spanning Tree

G is a weighted graph

T is the MST of G iff T is a spanning tree of G and
for any other spanning tree of G, C(T) < C(T’)
0
0
7
5
10
4
2
1
4
5
12
3
2
6
1
8
6
15
5
7
3
4
2
4
5
12
7
3
2
6
8
6
3
18
PNU
STEM
Finding MST

Greedy Algorithm


Choosing a next branch which looks like better than others.
Not always the optimal solution
Globally optimal solution
Solution by greedy algorithm,
only locally optimal
Current
state

Kruskal’s Algorithm and Prim’s Algorithm

Two greedy algorithms to find MST
19
PNU
STEM
Kruskal’s Algorithm
Algorithm KruskalMST
Input: Graph G
Output: MST T
Begin
T {};
while( n(T)<n-1 and G.E is not empty) {
(v,w) smallest edge of G.E;
G.E  G.E-{(v,w)};
if no cycle in {(v,w)}T, T {(v,w)}T
}
if(n(T)<n-1) cout<<“No MST”;
End Algorithm
0
7X
5
10
T is MST
Time Complexity: O(e loge)
1
X
4
2
4
5
12
7
3
2
6
8
6
15
3
20
PNU
STEM
Checking Cycles for Kruskal’s Algorithm
V
1
V1
V2
log n
5
6
4
2
3
If v  V and
8
w  V,
then  cycle,
otherwise no cycle
w
v
21
PNU
STEM
Prim’s Algorithm
Algorithm PrimMST
Input: Graph G
Output: MST T
Begin
TV {0}; T {};
while( n(T)<n-1) {
select v such that (u,v) is the smallest edge
where uTV, vTV and G.E  G.E-{(v,w)};
if no such v, break;
T {(u,v)}T;
TV {v}T;
}
if(n(T)<n-1) cout<<“No MST”;
End Algorithm
0
7
10
5
1
3
T is the MST
Time Complexity: O( n
4
2)
2
4
5
2
6
12
7
8
6
15
3
22
PNU
STEM
Finding the Edge with Min-Cost
Step 1
Step 2
T
V
TV
TV
0
0
1
1
3
3
2
2
n-1
n
n + (n-1) +… 1 = O( n2 )
23
PNU
STEM
Shortest Path Problem

Shortest Path

From 0 to all other vertices
0
5
vertex
cost
1
5
2
6
3
10
4
8
5
7
6
13
7
11
3
8
1
4
2
11
9
4
7
6
5
10
1
6
15
2
3
13
6
12
24
PNU
STEM
Finding Shortest Path from Single Source
(Nonnegative Weight)
2
0
Algorithm DijkstraShortestPath(G) /* G=(V,E) */
output: Shortest Path Length Array D[n]
Begin
S {v0};
D[v0]0;
for each v in V-{v0}, do D[v]  c(v0,v);
while S  V do
begin
choose a vertex w in V-S such that D[w] is minimum;
add w to S;
for each v in V-S do
D[v]  min(D[v],D[w]+c(w,v));
end
End Algorithm
1
7
10
3
6
2
4
3
5
4
w
v
u
S
O(
n2 )
25
PNU
STEM
Finding Shortest Path from Single Source
(Nonnegative Weight)
2
0
0
1
7
10
3
6
4
5
9
∞
2
4
0
1
9
2
4
3
3: {v0, v1, v2}
2: {v0, v1}
2
1
0
5
9
2
3
3
5
9
∞
4
1: {v0}
0
1
5
∞
4
2
2
0
1
10
2
3
2
9
2
4
2
1
5
9
9
4
3
2
3
4: {v0, v1, v2, v4}
5: {v0, v1, v2, v3, v4}
26
PNU
STEM
Transitive Closure

Transitive Closure

Set of reachable nodes
0
0
1
0
1
1
2
2
4
2
4
4
3
3
3
A+
A
A*
0
1
0
0
0
0
1
1
1
1
1
1
1
1
1
0
0
1
0
0
0
0
1
1
1
0
1
1
1
1
0
0
0
1
0
0
0
1
1
1
0
0
1
1
1
0
0
0
0
1
0
0
1
1
1
0
0
1
1
1
0
0
1
0
0
0
0
1
1
1
0
0
1
1
1
27
PNU
STEM
Transitive Closure
Void Graph::TransitiveClosure {
for(int i=0;i<n;i++)
for (int j=0;i<n;j++)
a[i][j]=c[i][j];
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for (int j=0;i<n;j++)
a[i][j]= a[i][j]||(a[i][k] && a[k][j]);
}
O( n3 )
Void Graph::AllPairsShortestPath {
for(int i=0;i<n;i++)
for (int j=0;i<n;j++)
a[i][j]=c[i][j];
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for (int j=0;i<n;j++)
a[i][j]= min(a[i][j],(a[i][k]+a[k][j]));
}
O( n3 )
28