Download presentation
Presentation is loading. Please wait.
Published byMichael Summers Modified over 9 years ago
1
#1© K.Goczyła GRAPHS Definitions and data structuresDefinitions and data structures Traversing graphsTraversing graphs Searching for paths in graphsSearching for paths in graphs
2
#2© 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.
3
#3© K.Goczyła Computer representations 1 2 3 4 5 6 1 2 3 6 4 5 1 2 3 4 5 6 1 0 1 1 0 0 0 2 0 0 0 0 0 0 3 0 1 0 1 0 0 4 0 0 0 0 0 0 5 0 0 0 1 0 1 6 0 0 0 0 1 0 1 2 3 4 5 6 1 0 1 1 0 0 0 2 0 0 0 0 0 0 3 0 1 0 1 0 0 4 0 0 0 0 0 0 5 0 0 0 1 0 1 6 0 0 0 0 1 0 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 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 Adjacency matrix Incidence lists 1: 2,3 2: 3: 2,4 4: 5: 4,6 6: 5 1: 2,3 2: 3: 2,4 4: 5: 4,6 6: 5 1: 2,3,5 2: 1,3,4 3: 1,2,5 4: 2,5,6 5: 1,3,4,6 6: 4,5 1: 2,3,5 2: 1,3,4 3: 1,2,5 4: 2,5,6 5: 1,3,4,6 6: 4,5
4
#4© K.Goczyła Traversing graphs: Depth-First Search 1 2 3 6 4 13 Incidence lists 1 : 2, 4, 12 2 : 1, 4 3 : 7 4 : 1, 2, 6, 7, 12 5 : 6, 8, 9 6 : 4, 5, 7, 9, 13 7 : 3, 4, 6 8 : 5, 9 9 : 5, 6, 8 10: 11, 12 11: 10, 12 12: 1, 4, 10, 11 13: 6 1 : 2, 4, 12 2 : 1, 4 3 : 7 4 : 1, 2, 6, 7, 12 5 : 6, 8, 9 6 : 4, 5, 7, 9, 13 7 : 3, 4, 6 8 : 5, 9 9 : 5, 6, 8 10: 11, 12 11: 10, 12 12: 1, 4, 10, 11 13: 6 10 12 7 11 5 8 9 STACK: 1, 2, 4, 6, 5, 8, 9, 7, 3, 13, 12 nnnnnnnnnnnnnnnnnnnnnnnnnn / / / / / / / / / // / / / / ///// /, 10, 11 /// 12 11 10 9 8 7 6 5 4 3 2 113 //
5
#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: Depth-First Search
6
#6© K.Goczyła 1 2 3 6 4 13 Incidence lists 1 : 2, 4, 12 2 : 1, 4 3 : 7 4 : 1, 2, 6, 7, 12 5 : 6, 8, 9 6 : 4, 5, 7, 9, 13 7 : 3, 4, 6 8 : 5, 9 9 : 5, 6, 8 10: 11, 12 11: 10, 12 12: 1, 4, 10, 11 13: 6 1 : 2, 4, 12 2 : 1, 4 3 : 7 4 : 1, 2, 6, 7, 12 5 : 6, 8, 9 6 : 4, 5, 7, 9, 13 7 : 3, 4, 6 8 : 5, 9 9 : 5, 6, 8 10: 11, 12 11: 10, 12 12: 1, 4, 10, 11 13: 6 10 12 7 11 5 8 9 QUEUE: 1, 2, 4, 6, 5, 8, 9, 7, 3, 13, 12 nnnnnnnnnnnnnnnnnnnnnnnnnn / / / / / / / / / // / / / / ///// /, 10, 11 /// 12 11 10 9 8 7 6 5 4 3 2 113 // 1 Path 12 5 1 4 4 1 6 6 6 7 - Traversing graphs: Breadth-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,8 to 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
7
#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: Breadth-First Search
8
#8© 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) = . 0 1 3 / k D[1] D[2] D[3] D[4] D[5] s=1 2 5 3 4 (3) (1) (4) (8) (1)(2) (3) (-5) 1 Matrix 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 1234512345 1 0 223 2 5 3 Paths: to 2: 1, 2 144 3 to 3: 1, 2, 3 to 5: 1, 2, 3, 5 to 4: 1, 2, 3, 5, 4 no change
9
#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(n 3 ) Finding shortest paths - Ford-Bellman algorithm
10
#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 222 6 (1) 2 1 3 4 6 5 (2) (1) (5) (7) (4) (3) (1) 4 7 8 4 2 1 7 5 / / 3 4 2 1 4 / 6 3 4 2 1 1 D[1] D[2] D[3] D[4] D[5] D[6] Paths T = { 2, 3, 4, 5, 6 } /// / / Finding shortest paths - Dijkstra algorithm
11
#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 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 } 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 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 Finding shortest paths - Dijkstra algorithm Note: The order of computation complexity does not change if we find the shortest path only to one specific vertex.
12
#12© 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.
13
#13© K.Goczyła Finding an Euler’s cycle in a graph with no vertices of odd degree 1 3 7 4 Incidence lists 1 : 2, 3 2 : 1, 3, 7, 8 3 : 1, 2, 4, 5 4 : 3, 5 5 : 3, 4, 6, 8 6 : 5, 7, 8, 9, 7 : 2, 6, 8, 9 8 : 2, 5, 6, 7 9 : 6, 7 1 : 2, 3 2 : 1, 3, 7, 8 3 : 1, 2, 4, 5 4 : 3, 5 5 : 3, 4, 6, 8 6 : 5, 7, 8, 9, 7 : 2, 6, 8, 9 8 : 2, 5, 6, 7 9 : 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 x x x / / // // /// / / / x x x / // x ////// /// ///
14
#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 cycle
15
#15© K.Goczyła 3 5 2 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, 4 or: Finding an Euler’s path in a graph with 2 vertices of odd degree
16
#16© 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. Graph that has a Hamilton’s pathGraph 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.