Research Group Software Engineering

Download Report

Transcript Research Group Software Engineering

GRAPHS
• Definitions and data structures
• Traversing graphs
• Searching for paths in graphs
#1
© K.Goczyła
Definitions
Graph:
G = V, E,
where V is a set of vertices, E is a set of edges (arcs).
E  VV
for directed graph (set od arcs)
E  { {x, y}: x, y  V  x  y}
for undirected graph (set of edges)
|V| = n, |E| = m
Undirected graph:
Directed graph:
Degree of a vertex: the number of direct neighbors.
The two exemplary graphs are connected.
#2
© K.Goczyła
Computer representations
Adjacency matrix
2
5
1
4
3
2
#3
1
2
3
4
5
6
1
0
0
0
0
0
0
2
1
0
1
0
0
0
3
1
0
0
0
0
0
4
0
0
1
0
1
0
5
0
0
0
0
0
1
6
0
0
0
0
1
0
1:
2:
3:
4:
5:
6:
2,3
1
2
3
4
5
6
1
0
1
1
0
1
0
2
1
0
1
1
0
0
3
1
1
0
0
1
0
4
0
1
0
0
1
1
5
1
0
1
1
0
1
6
0
0
0
1
1
0
1:
2:
3:
4:
5:
6:
2,3,5
1,3,4
1,2,5
2,5,6
1,3,4,6
4,5
2,4
4,6
5
6
4
1
6
3
Incidence lists
5
© K.Goczyła
Traversing graphs:
Depth-First Search
3
9
2
Incidence lists
2
8
7
5 5
1
6
1
8
4
11
3
4
12
13
10
6
7
9
10
11
12
13
n
/
n
/
n
/
n
/
n
/
n
/
n
/
n
/
n
/
/
n
n
/
/
n
n
/
1 :
2 :
3 :
4 :
5 :
6 :
7 :
8 :
9 :
10:
11:
12:
13:
2, 4, 12
1, 4
7
1, 2, 6, 7, 12
6, 8, 9
4, 5, 7, 9, 13
3, 4, 6
5, 9
5, 6, 8
11, 12
10, 12
1, 4, 10, 11
6
1, /2 , 4/, 6,
5, 8,
/ /
/ 9,
/ /7, 3/, 13,
/ 12
/ , 10
/ , 11
/
STACK: /
#4
© K.Goczyła
Traversing graphs:
Depth-First Search
Recursive procedure (an outline):
/* IL – incidence lists, NEW – table of visit tags */
Procedure DFSearch (in v: node)
/* traverse the graph by DFS, starting from vertex v */
{ visit(v);
NEW[v] = false;
/* mark that the vertex v has already been visited */
for u  IL[v] do
if NEW[u] then DFSearch(u)
} /* vertex v has been visited*/
/* main program */
/* input: incidence lists IL */
{ for v  V do NEW[v] = true;
/* initialize visit tags */
for v  V do
if NEW[v] then DFSearch(v)
}
Computational complexity: O(m+n)
#5
© K.Goczyła
Traversing graphs:
Breadth-First Search
3
12
2
Path
2
6
7
5 9
5
1
13
6
1
8
4
4
3
12
9 10
13 11
10
#6
11
1
7
1
6
4
4
5
6
12
12
1
6
7
8
QUEUE:
/, /2 , 4/, 12
1
/ , 6/, 7/, 10,
/
/, 5/, 9/, 13
/ , 3/, 8/
11
Paths:
to 2: 1, 2;
to 3: 1,4,7,3
to 4: 1,4;
to 5: 1,4,6,5
to 6: 1,4,6;
to 7: 1,4,7;
to 8: 1,4,6,5,8
to 9: 1,4,6,9
Incidence lists
n
/
n
/
/
n
n
/
/
n
n
/
n
/
n
/
n
/
/
n
n
/
/
n
n
/
1 :
2 :
3 :
4 :
5 :
6 :
7 :
8 :
9 :
10:
11:
12:
13:
2, 4, 12
1, 4
7
1, 2, 6, 7, 12
6, 8, 9
4, 5, 7, 9, 13
3, 4, 6
5, 9
5, 6, 8
11, 12
10, 12
1, 4, 10, 11
6
to 10: 1,12,10;
to 11: 1,12,11;
to 12: 1,12;
to 13: 1,4,6,13
© K.Goczyła
Traversing graphs:
Breadth-First Search
Iterative procedure (an outline):
/* IL – incidence lists,
NEW – table of visit marks,
PATH – path of graph search */
Procedure BFSearch (in v: node)
/* traverse the graph by BFS, starting from vertex v */
{ QUEUE = ; QUEUE = v; /* initialize the queue */
NEW[v] = false;
/* mark that the vertex v has already been visited */
while QUEUE !=  do
{ p = QUEUE; visit(p);
for u  IL[p] do
if NEW[u] then
{ QUEUE = u;
NEW = false;
PATH[u] = p
}
}
}
Computational complexity: O(m+n)
#7
© K.Goczyła
Finding shortest paths Ford-Bellman algorithm
Assumptions:
For any arc u, v  V of a directed graph there exists a number a(u, v) called the weigth of the arc.
In the graph there are no cycles of negative length (sum of weigths).
If there is no arc from u to v, we assume a(u, v) =  .
2
(1)
Matrix of weigths
(3)
1
(2)
(8)
s=1
3
(3)
2
(1)
3
(-5)
4
(3)
5
5
(4)
1
2
3
4





1





3

2

 3
3 8
1 -5
 
4 
4
1
k
Paths:
to 2:
to 3:
to 5:
to 4:
1, 2
1, 2, 3
1, 2, 3, 5
1, 2, 3, 5, 4
1
2
5
/
2
3
D[1] D[2] D[3] D[4] D[5]
0
1


0
1
4
4
3
-1
3
2
3
#8
5
no change
© K.Goczyła
Finding shortest paths Ford-Bellman algorithm
Procedure (an outline):
Input:
A
s
Output: D
P
–
–
–
–
matrix of arc weigths for a directed graph with no cycles of negative length
starting vertex (source)
distances from the source to all vertices of the graph
lists of paths from the source to all vertices of the graph
Procedure Ford-Bellman (in s: node)
{for v  V do D[v] = A[s,v]; D[s] = 0;
/* initialization */
for k = 1 to n-2 do
for v  V–{s} do
for u  V do
{ D[v]= min(D[v], D[u]+A[u,v]);
if there was a change, then store the previous vertex on the path
}
}
Computational complexity: O(n3)
#9
© K.Goczyła
Finding shortest paths Dijkstra algorithm
Paths
Assumptions:
For any arc u, v  V of a directed graph there exists a non-negative number a(u, v) called the weigth of the arc.
For an undirected graph we assume a(u, v) = a(v, u).
If there is no arc from u to v, we assume a(u, v) =  .
(7)
1
2
3
(5)
(2)
(1)
6
(1)
(1)
(1)
(4)
1
2
4
2
/
1
4
(3)
1
2
4
3
2
/
D[1] D[2] D[3] D[4] D[5] D[6]
0
1


6
3
4
(2)
1
2
1
2
4
3
6
4
/



8
7
8
7
5
5
6
T = { 2,
/ 3,
/ 4,
/ 5,
/ 6/ }
#10
© K.Goczyła
Finding shortest paths Dijkstra algorithm
Procedure (an outline):
Input:
A
s
Output: D
P
–
–
–
–
matrix of non-negative arc weigths ,
starting vertex (source)
matrix of distances from the source to all vertices of the graph
lists of paths from the source to all vertices of the graph
Procedure Dijkstra (in s: node)
{for v  V do D[v] = A[s,v]; D[s] = 0;
/* initialization */
T = V–{s};
while T !=  do
{ u = any vertex rT such that D[r] is minimal;
Store the path basing on the path to the previous vertex;
T = T–{u};
for v  T do
{ D[v]= min(D[v], D[u]+A[u,v]);
if there was a change, then store the previous vertex on the path
}
}
}
Computation complexity: O(m log n) – after some optimization
Note:
The order of computation complexity does not change if we find the shortest path only to one specific vertex.
#11
© K.Goczyła
Finding Euler’s path in a graph
Euler’s path:
any path that traverses each edge of the graph exactly once.
Euler’s cycle:
Euler’s path where the starting and ending vertices are the same.
Theorem:
An Euler’s path in a graph exists if and only if the graph is connected
and contains no more than 2 vertices of odd degree.
These vertices are the endpoints of the Euler’s path.
„Closed envelope” – no Euler’s path
„Open envelope” has an Euler’s path
Note:
If a connected graph does not have vertices of odd degree,
then each Euler’s path is a cycle.
#12
© K.Goczyła
Finding an Euler’s cycle in a graph
with no vertices of odd degree
Incidence lists
4
3
1
5
x
6
8
9
2
1
2
3
4
5
6
7
8
9
:
:
:
:
:
:
:
:
:
2,
/
/
1,
/
1,
/
3,
3,
/
/
5,
2,
/
/
2,
6,
/
3
/
/
3,
/
2,
/
5
4,
/
/
7,
6,
/
/
5,
7
/
/ 8/
7,
/
4, 5/
6,
/
/
8,
8,
/
/
6,
8/
/
9,
9/
7/
7
/, /5, 6,
/ 9,
/ /
/ /
1 , 4,
/ /5, /3, 6,
/ /7, 2,
/ 8
7, /8
STACK: /1, /2 , 3,
EC:
#13
1, 3 , 5, 8 , 7, 9, 6, 8, 2, 7, 6, 5, 4, 3, 2, 1
© K.Goczyła
Finding an Euler’s cycle
Procedure (an outline):
Input: IL – incidence lists of a connected graph with no vertices of odd degree,
Output: EC – Euler’s cycle in the graph
Procedure Euler
{ STACK = ; EC = ;
/* initialization */
v = any vertex;
STACK <- v;
/* push v */
while STACK !=  do
if IL[v] !=  then
{ u = first node from list IL[v];
STACK <- u;
/* push u */
IL[v]=IL[v]–{u}; IL[u]=IL[u]-{v};
/* remove edge {v,u} from the graph */
v = u
}
else
/* IL[v] is already empty */
{ v <- STACK;
/* pop v */
CE <- v
/* append v to Euler’s cycle */
}
}
Computational complexity: O(m)
#14
© K.Goczyła
Finding an Euler’s path in a graph
with 2 vertices of odd degree
3
2
4
5
6
1
1. We add an edge between the two vertices of odd degree.
2. We apply the Euler procedure:
EC:
1, 6, 4, 5, 2, 4, 3, 2, 1
3. We change the Euler’s cycle into the Euler’s path:
EP:
or:
#15
4, 3, 2, 1, 6, 4, 5, 2
2, 5, 4, 6, 1, 2, 3, 4
© K.Goczyła
Finding a Hamilton’s path in a graph
Hamilton’s path:
any path that visits each vertex of a graph exactly once.
Hamilton’s cycle:
a Hamilton’s path where the starting and ending vertices are the same.
Finding a Hamilton’s path/cycle is computationally hard – there is no known algorithm that finds
a Hamilton’s path in a time that is polynomially dependent on the number of vertices n.
3
6
4
1
7
2
5
Graph that has a Hamilton’s path
Graph with no Hamilton’s path
Searching for a shortest Hamilton’s cycle in a weighted graph is called
Travelling salesman problem.
#16
© K.Goczyła