#1© k.goczyła graphs definitions and data structuresdefinitions and data structures traversing...

16
#1 © K.Goczyła GRAPHS GRAPHS Definitions and data structures Definitions and data structures Traversing graphs Traversing graphs Searching for paths in graphs Searching for paths in graphs

Upload: michael-summers

Post on 16-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#1 © K.Goczyła

GRAPHSGRAPHS

• Definitions and data structuresDefinitions and data structures• Traversing graphsTraversing graphs• Searching for paths in graphsSearching for paths in graphs

Page 2: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#2 © K.Goczyła

DefinitionsDefinitions

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.

Page 3: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#3 © K.Goczyła

Computer representationsComputer representations

1

2

3

4

5

6

1

2

3

6

4

5

1 2 3 4 5 61 0 1 1 0 0 02 0 0 0 0 0 03 0 1 0 1 0 04 0 0 0 0 0 05 0 0 0 1 0 16 0 0 0 0 1 0

1 2 3 4 5 61 0 1 1 0 0 02 0 0 0 0 0 03 0 1 0 1 0 04 0 0 0 0 0 05 0 0 0 1 0 16 0 0 0 0 1 0

1 2 3 4 5 61 0 1 1 0 1 02 1 0 1 1 0 03 1 1 0 0 1 04 0 1 0 0 1 15 1 0 1 1 0 16 0 0 0 1 1 0

1 2 3 4 5 61 0 1 1 0 1 02 1 0 1 1 0 03 1 1 0 0 1 04 0 1 0 0 1 15 1 0 1 1 0 16 0 0 0 1 1 0

Adjacency matrixAdjacency matrix Incidence listsIncidence lists

1: 2,32:3: 2,44:5: 4,66: 5

1: 2,32:3: 2,44:5: 4,66: 5

1: 2,3,52: 1,3,43: 1,2,54: 2,5,65: 1,3,4,66: 4,5

1: 2,3,52: 1,3,43: 1,2,54: 2,5,65: 1,3,4,66: 4,5

Page 4: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#4 © K.Goczyła

Traversing graphs:Traversing graphs:Depth-First SearchDepth-First Search

1

2

3

6

4

13

Incidence listsIncidence lists

1 : 2, 4, 122 : 1, 43 : 74 : 1, 2, 6, 7, 125 : 6, 8, 96 : 4, 5, 7, 9, 137 : 3, 4, 68 : 5, 99 : 5, 6, 810: 11, 1211: 10, 1212: 1, 4, 10, 1113: 6

1 : 2, 4, 122 : 1, 43 : 74 : 1, 2, 6, 7, 125 : 6, 8, 96 : 4, 5, 7, 9, 137 : 3, 4, 68 : 5, 99 : 5, 6, 810: 11, 1211: 10, 1212: 1, 4, 10, 1113: 6

10

12

7

11

5

8

9

STACK: 1, 2, 4, 6, 5, 8, 9, 7, 3, 13, 12

nnnnnnnnnnnnn

//

/

//

/

/

/

/

//

/

/

/

/

/////

/

, 10, 11///

12

11

10

9

8

7

6

5

4 3

2

1

13

//

Page 5: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#5 © K.Goczyła

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)}

/* 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)

Traversing graphs:Traversing graphs:Depth-First SearchDepth-First Search

Page 6: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#6 © K.Goczyła

1

2

3

6

4

13

Incidence listsIncidence lists

1 : 2, 4, 122 : 1, 43 : 74 : 1, 2, 6, 7, 125 : 6, 8, 96 : 4, 5, 7, 9, 137 : 3, 4, 68 : 5, 99 : 5, 6, 810: 11, 1211: 10, 1212: 1, 4, 10, 1113: 6

1 : 2, 4, 122 : 1, 43 : 74 : 1, 2, 6, 7, 125 : 6, 8, 96 : 4, 5, 7, 9, 137 : 3, 4, 68 : 5, 99 : 5, 6, 810: 11, 1211: 10, 1212: 1, 4, 10, 1113: 6

10

12

7

11

5

8

9

QUEUE: 1, 2, 4 , 6 , 5 , 8, 9, 7 , 3, 13, 12

nnnnnnnnnnnnn

//

//

/

/

/

/

/

//

/

//

/

/////

/

, 10, 11 ///

12

11

10

9

8 7

6

5

4

3

2

1 13

//

1

PathPath

12

5

1

44

16

612

6

7

-

Traversing graphs:Traversing graphs:Breadth-First SearchBreadth-First Search

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,8to 9: 1,4,6,9

to 10: 1,12,10; to 11: 1,12,11;to 12: 1,12; to 13: 1,4,6,13

Page 7: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#7 © K.Goczyła

/* 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 } } }

/* 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)

Iterative procedure (an outline):

Traversing graphs:Traversing graphs:Breadth-First SearchBreadth-First Search

Page 8: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#8 © K.Goczyła

Finding shortest paths -Finding shortest paths -Ford-Bellman algorithmFord-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) = .

0 1 3

/

k D[1] D[2] D[3] D[4] D[5]

s=1

2

5

3

4

(3)

(3)

(1)

(4)

(8) (1)(2)

(3)

(-5)

1

Matrix of weigthsMatrix of weigths

1 3 3 3 8 1 -5 2 4

1 3 3 3 8 1 -5 2 4

1 2 3 4 5

1

2

3

4

5

1 0

2 2 3

2

5

3

Paths:

to 2: 1, 2

1 4 4 -1

3

to 3: 1, 2, 3to 5: 1, 2, 3, 5to 4: 1, 2, 3, 5, 4

no change

Page 9: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#9 © K.Goczyła

Procedure (an outline):

Input: A – matrix of arc weigths for a directed graph with no cycles of negative length s – starting vertex (source)Output: D – distances from the source to all vertices of the graph P – 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 } }

Input: A – matrix of arc weigths for a directed graph with no cycles of negative length s – starting vertex (source)Output: D – distances from the source to all vertices of the graph P – 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)

Finding shortest paths -Finding shortest paths -Ford-Bellman algorithmFord-Bellman algorithm

Page 10: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#10 © K.Goczyła

1

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) = .

0 1

6 3 8

22 2

6

(1)

2

1

3

4

6

5

(2)

(2)

(1)

(1)(5)

(7)

(4)

(3)

(1)

4 7 8

42

1

7 5

/ /

3

42

1

4/

6

3

42

1

1

D[1] D[2] D[3] D[4] D[5] D[6]

Pat

hs

T = { 2, 3, 4, 5, 6 } / / / / /

Finding shortest paths -Finding shortest paths -Dijkstra algorithmDijkstra algorithm

Page 11: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#11 © K.Goczyła

Procedure (an outline):

Input: A – matrix of non-negative arc weigths , s – starting vertex (source)Output: D – matrix of distances from the source to all vertices of the graph P – 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 rT 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 } } }

Input: A – matrix of non-negative arc weigths , s – starting vertex (source)Output: D – matrix of distances from the source to all vertices of the graph P – 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 rT 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

Finding shortest paths -Finding shortest paths -Dijkstra algorithmDijkstra algorithm

Note: The order of computation complexity does not change if we find the shortest path only to one specific vertex.

Page 12: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#12 © K.Goczyła

Finding Euler’s path in a graphFinding 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 connectedand 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.

Page 13: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#13 © K.Goczyła

Finding an Euler’s cycle in a graphFinding an Euler’s cycle in a graphwith no vertices of odd degreewith no vertices of odd degree

1

3

7

4

Incidence listsIncidence lists

1 : 2, 32 : 1, 3, 7, 83 : 1, 2, 4, 54 : 3, 55 : 3, 4, 6, 86 : 5, 7, 8, 9,7 : 2, 6, 8, 98 : 2, 5, 6, 79 : 6, 7

1 : 2, 32 : 1, 3, 7, 83 : 1, 2, 4, 54 : 3, 55 : 3, 4, 6, 86 : 5, 7, 8, 9,7 : 2, 6, 8, 98 : 2, 5, 6, 79 : 6, 7

6

9

8

STACK: 1, 2, 3, 1 , 4, 5, 3, 6, 7, 2, 8

/

/

5

2

EC:

x

/ /

x

x

/

//

x

// /

//

///

, 5, 6, 9, 7, 8

1, 3, 5, 8 , 7, 9, 6, 8, 2, 7, 6, 5, 4, 3, 2, 1

x/

/ /

/

x

x

x

xx

x

/

/

//

/ // //

/

/

/

x

x

x

/

//x

/ /// / / / / /// /

Page 14: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#14 © K.Goczyła

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 */ } }

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)

Finding an Euler’s cycleFinding an Euler’s cycle

Page 15: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#15 © K.Goczyła

3

52

1

4

6

EC: 1, 6, 4, 5, 2, 4, 3, 2, 1

1. We add an edge between the two vertices of odd degree.

2. We apply the Euler procedure:

3. We change the Euler’s cycle into the Euler’s path:

EP: 4, 3, 2, 1, 6, 4, 5, 2

2, 5, 4, 6, 1, 2, 3, 4or:

Finding an Euler’s path in a graphFinding an Euler’s path in a graphwith 2 vertices of odd degreewith 2 vertices of odd degree

Page 16: #1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching

#16 © K.Goczyła

Finding a Hamilton’s path in a graphFinding 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 findsa Hamilton’s path in a time that is polynomially dependent on the number of vertices n.

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.

1

2

3

4

5

6

7