dad2b4154a384fdeef3c7d418ea54f67.ppt
- Количество слайдов: 144
Computing Fundamentals 2 Lecture 2 A theory of Graphs Lecturer: Patrick Browne http: //www. comp. dit. ie/pbrowne/ Lecture Room K 408, Labs A 305 & A 306 Based on Chapter 19. A Logical approach to Discrete Math By David Gries and Fred B. Schneider
Graph theory began with Euler’s study of a particular problem: “ the Seven. Bridges of K¨onigsberg. “ During the eighteenth century the city of K¨onigsberg (in East Prussia) was divided into four sections (including the island of Kneiphof) by the Pregel river. Seven bridges connected these regions and it was said that residents spent their Sunday walks trying to find a starting point so that they could walk about the city, cross each bridge exactly once, and return to their starting point.
A Theory of Graphs • • • In this lecture we look at: Graph Morphisms Hamilton Circuits Planar Graphs Shortest Path
Graph Morphisms • The two graphs below look different but are f. c structurally the ‘same’. b 2(c) f. a 1(a) a c e 3(e) f. d 5(d) d f. b 4(b) f. e • They are the ‘same’ up to the renaming of the vertices. These graphs are isomorphic.
Graph Isomorphism • Given two graphs G and G’ with vertices V and V’. The graphs are isomorphic if there exists a 1 -to-1 function f: V->V’ such that: • An isomorphism f “preserves edges” thus preserving properties of graphs that depend on edges e. g. cycles and degrees are preserved.
Isomorphism Invariant • An isomorphic invariant for simple graphs is the existence of a simple circuit of length k , where k is an integer > 2. Example: G 1 and G 2 are isomorphic since we have the invariants, similarity in degree of nodes, number of edges, length of circuits. G 1 G 2
Graph Isomorphism • Sometimes we can check whether two graphs are not isomorphic. The left graph below has two vertices of degree 3 while the right one has none. • It is much harder to prove that two graphs are isomorphic. The computation is time is exponential wrt. the number of vertices.
Homeomorphic Graphs • Two graphs are homeomorphic if they can both be obtained from the same graph by inserting or removing vertices of degree 2. 1 2 3 b e c d • Adding b to 1 gives 2 • Adding c, d, e, to 1 gives 3 • Hence 2 and 3 are homeomorphic because they were obtained from 1 by adding (removing) vertices of degree 2.
Recall: Euler and Hamiltonian Cycles • An Euler cycle is a cycle in a graph that includes each edge exactly once. Examples: Designing and optimizing routes refuse trucks, snow ploughs, or postmen. In all of these applications, every edge in a graph must be traversed at least once. • A Hamiltonian cycle is a cycle in a graph G that contains each vertex exactly once except for the starting and ending vertex that appears twice. Examples: travelling sales man who wishes to visit every city and also minimize his route to each city and return to his home city.
Hamilton Circuit • A Hamilton path of a graph or digraph is a path that contains each vertex exactly once, except that the end vertices may be the same. • A Hamilton circuit (or cycle) is a Hamilton path that is a cycle. • Contrast this with an Euler circuit which contains each edge exactly once.
Hamilton Path and Circuit • G has Hamilton path but no Hamilton circuit (or cycle).
Hamiltonian cycles • Traveling salesperson problem – To visit every vertex of a graph G only once by a simple cycle. – Such a cycle is called a Hamilton cycle. – If a connected graph G has a Hamilton cycle, G is called a Hamiltonian graph.
Hints for finding Hamilton Circuits • If G has a HC, then deg. v >= 2 for all v on HC. • If deg. v = 2 then both edges incident on v are in HC. • If deg. v > 2 and two of the edges incident on v are in a HC, then the other edges incident on v are not in that HC. • A complete digraph (v>2) has a Hamilton Path.
Hints for finding Hamilton Circuits
Hints for finding Hamilton Circuits
Hamilton Circuit • Find a Hamilton circuit in the G. b a d c e i f g j h
Hamilton Circuit • Find a Hamilton circuit in the G. b a d c e i Solution: d, a f g j h
Hamilton Circuit • Find a Hamilton circuit in the G. b a d c e i Solution: d, a, e f g j h
Hamilton Circuit • Find a Hamilton circuit in the G. b a d c e i Solution: d, a, e, b f g j h
Hamilton Circuit • Find a Hamilton circuit in the G. b a d c e i Solution: d, a, e, b, g f g j h
Hamilton Circuit • Find a Hamilton circuit in the G. b a d c e i Solution: d, a, e, b, g, c f g j h
Hamilton Circuit • Find a Hamilton circuit in the G. b a d c e i Solution: d, a, e, b, g, c, h f g j h
Hamilton Circuit • Find a Hamilton circuit in the G. b a d c e i Solution: d, a, e, b, g, c, h, j f g j h
Hamilton Circuit • Find a Hamilton circuit in the G. b a d c e f i Solution: d, a, e, b, g, c, h, j, f g j h
Hamilton Circuit • Find a Hamilton circuit in the G. b a d c e f i Solution: d, a, e, b, g, c, h, j, f, i g j h
Hamilton Circuit • Find a Hamilton circuit in the G. b a d c e f i Solution: d, a, e, b, g, c, h, j, f, i, d g j h
Hamilton Circuit • Find another Hamilton circuit in the same graph G. b a d c e i f g h j • Note that while verticies a and b were on previous Hamilton circuit the edge
Hamilton Circuit • Find another Hamilton circuit in the G. b a d c e i f g j h
Hamilton Circuit • Find another Hamilton circuit in the G. b a d c e i f g j h
Hamilton Circuit • Find another Hamilton circuit in the G. b a d c e i f g j h
Hamilton Circuit • Find another Hamilton circuit in the G. b a d c e i f g j h
Hamilton Circuit • Find another Hamilton circuit in the G. b a d c e i f g j h
Hamilton Circuit • Find another Hamilton circuit in the G. b a d c e i f g j h
Hamilton Circuit • Find another Hamilton circuit in the G. b a d c e i f g j h
Hamilton Circuit • Find another Hamilton circuit in the G. b a d c e i f g j h
Hamilton Circuit • Find another Hamilton circuit in the G. b a d c e i f g j h
Is HC unique? • There are graphs with unique Hamiltonian cycles:
Two classes of Hamiltonian Paths 1. A complete digraph (v>2) has a Hamiltonian path. 2. Let G be an undirected graph of n Hamiltonian path vertices. If deg. b + deg. c ≥ n-1 for each pair of vertices b, c then G has a Hamiltonian path. Note direction of implication b, c(deg. b + deg. c ≥ n-1) HP If 2 does not hold we do not know about HP
Showing there is no HC • A HC must contain exactly two edges incident on each vertex. Hence each vertex v on the HC has degree(v)= 2. • In any graph sum(degree(v))=2*|E|. In the HC case |V|*2=2*|E|, which reduces to |V|=|E|. Since G (below) has 5 vertices then the HC should have 5 edges. v 1 v 5 v 4 v 3 v 2 Graph G
Showing there is no HC Graph G v 1 • Looking just at the HC in G, each vertex on the HC has degree two. We now eliminate edges incident on vertices with degree v 4 greater than two. • The edges
Showing there is no HC • Using this method we must avoid double counting. We must not count an eliminated edges more than once. In this case, when we eliminated one edge incident on v 2 and one edge incident on v 4 these edges are distinct, and hence our argument is valid. v 1 v 5 v 2 v 3 Graph G; valid reasoning
Incorrect claim: G has no HC. In G 2 there are 5 vertices, hence HC must have 5 edges. Following the method from previous slide we eliminate two edges incident at v 5 and one edge incident at each v 1, v 2, v 3, and v 4, leaving just 2 v 4 edges, hence no HC. This is incorrect reasoning, because the red edges are counted twice. When we eliminate 2 edges incident on v 5 (which we must) we also eliminate edges incident on another vertices. Then we only need to remove 1 other edge. Now we have 5 edges and 5 vertices, hence a HC –correct reasoning. v 1 v 5 G 2 v 3 v 1 v 5 v 4 v 2 v 3
Showing there is no HC • How could we show that there is no HC in the graph below? a c b e d g f i h l m j k
Showing there is no HC • Suppose HC=H, then edges , , ,
Showing there is no HC • Therefore edges
Hamilton Cycles & Paths
Hamilton Cycles in K 4
Hamilton Cycles in K 4
Summary of Euler and Hamilton cycles • A Euler cycle is a cycle in a graph that includes all of the edges and all of the vertices in that cycle. • A Hamilton cycle is a cycle in a graph G the contains each vertex exactly once except for the starting and ending vertex that appears twice. • Recall: • A path is a sequence of verticies and edges but with no edge traversed more than once • A path with at least one edge and with the first and last vertices the same is called a cycle.
Summary of Euler and Hamilton cycles B D F C A B E • G has a Euler cycle but no Hamilton cycle
Summary of Euler and Hamilton cycles
Test for Euler cycle • If a graph G has an Euler cycle, then G is connected and every vertex has even degree. • If G is a connected graph and every vertex has even degree, then G has a Euler cycle.
a b f d c This graph G does not have a Euler cycle, since there are vertices of odd degree, yet G has a Hamiltonian cycle of: (a, b, c, d, e, f, g, a). g e Unlike Euler cycles, no easily verified necessary and sufficient conditions are known to exist for a Hamilton cycle in a graph edge(a, b), edge(a, f), edge(a, g), edge(a, e), edge(b, f), edge(b, d), edge(b, c), edge(c, d), edge(c, e), edge(e, d), edge(e, f), edge(e, g), edge(g, f)
This graph has a Euler cycle but no Hamiltonian cycle.
Cycle or Path? If a graph has a Hamilton cycle does it have to have a Hamilton path? Answer Yes: HC implies HP. If
. A Hamiltonian cycle exists on a complete graph • Recall, a Hamilton cycle is a cycle in a graph G the contains each vertex exactly once except for the starting and ending vertex that appears twice. • Recall, a complete graph on n vertices, Kn, is the simple graph (no loops or parallel edges) with n vertices in which there is an edge between every pair of distinct vertices. • Since there is an edge between every pair of vertices and this edge may be traversed in either direction, it follows that any permutation of vertices is a Hamilton path.
A Hamilton cycle exists on a complete graph. • Moreover, a Hamilton cycle is obtained by including the edge from the final to the initial vertex of such a path. • So in summary • Every complete graph has a Hamilton path and a Hamilton cycle.
Hamiltonian cycles can take a long time to compute! • For a general graph, every known algorithm for finding Hamiltonian cycles requires factorial time in the worst case. For a complete undirected graph with n vertices, there a possible (n-1)!/2 essentially different Hamiltonian cycles. The number of addition operations required to obtain the lengths of these cycles is O(n!). For example for n=12 it will take 5 seconds to perform the computation, for n=50 it will take 1049 years (assuming 108 (100, 000, or 100 million) additions per second). For this reason, methods that produce near-minimumlength cycles are often used.
Planar Graphs • A graph is called planar if it can be drawn on the plane in a way that no two of its edges cross each other
Planar graphs A graph is planar if it can be drawn in the plane without crossing edges
Planar graphs K 4 can be drawn with no edges crossing. A planar graph the points in the plane into regions or faces. Any planar embedding of a given graph has the same numbers of faces.
Planar graphs Two points (not vertices) are in the same region iff it is possible to draw an edge (curved) between them without touching an edge of the region. Each interior region (e. g. C and B) is characterized by a cycle that forms its boundary. The exterior region (D) contains all the points not bounded by some cycle.
Euler’s Planar Graph Theorem Euler’s Characteristic Theorem : Euler's planar graph theorem For all connected planar graph or multigraph: v–e+r=2 number of vertices number of edges number of regions or faces
Recall the Handshaking Theorem: Let G=(V, E) be an undirected graph. Then
Example of Handshaking Theorem • Draw a graph with 4 edges and 4 vertices , having degrees 1, 2, 3, 4. • Solution: No such graph exists. The sum of the degrees of all the vertices should be equal to twice the number of edges. Half of the sum of the degrees is 5, which is not equal to actual number of edges required in the question (4). • (1+2+3+4)/2 = 5, 5 =/= 4.
Planar Graphs Example where Euler’s theorem holds A planar graph divides the plane into several regions (faces), one of them is the infinite external region. • v=4, e=5, r=3, • v-e+r=2 • 4 -5+3=2
Planar graphs A graph (or multigraph) G is called planar if G can be drawn in the plane with its edges intersecting only at vertices of G, such a drawing of G is called an embedding of G in the plane. Used in VLSI design, where overlapping edges requires extra layers, wires cannot overlap on the circuit board. Representation examples: K 1, K 2, K 3, K 4 are planar, Kn for n>4 are non-planar
Edges in series: • If v V(G) has degree 2 and there are edges (v, v 1), (v, v 2) with v 1 v 2, • we say the edges (v, v 1) and (v, v 2) are in series.
Series reduction • A series reduction consists of deleting the vertex v V(G) and replacing the edges (v, v 1) and (v, v 2) by the edge (v 1, v 2) • The new graph G’ has one vertex and one edge less than G and is said to be obtained from G by series reduction
Homeomorphic graphs • Two graphs G and G’ are said to be homeomorphic if G’ is obtained from G by a sequence of series reductions. – By convention, G is said to be obtainable from itself by a series reduction, i. e. G is homeomorphic to itself. • Define a relation R on graphs: GRG’ if G and G’ are homeomorphic. • R is an equivalence relation on the set of all graphs.
Kuratowski’s theorem • G is a planar graph if and only if G does not contain a sub-graph homeomorphic to either K 5 or K 3, 3
Utility Problem(again) • There are three houses and three utility companies--say, gas, electric, and water--and asks if each utility can be connected to each house without having any of the gas-water-electric lines pass over any other. This is equivalent to the equation "Can a planar graph be constructed from each of three nodes ('houses') to each of three other nodes ('utilities')? " No such planar graph exists.
Not planar Here is an example of a graph which doesn't have K 5 or K 3, 3 as its subgraph. However, it has a subgraph that is homeomorphic to K 3, 3 (replace red lines with yellow line) and is therefore not planar. A finite graph is planar if and only if it does not contain a subgraph that is homeomorphic to K 5 or K 3, 3.
Euler’s formula • We can test if a graph is non-planar, but there is no simple check for planarity. If v is the number of vertices (at least 3) and e is the number of edges, then (e 3 v− 6 ) is a necessary (but not sufficient) condition for planarity. • That is, every connected planar graph satisfies (e 3 v− 6 ), but there are graphs that satisfy that constraint which are not planar. • Planar => (e 3 v− 6 )
Euler’s formula • Euler’s formula forms the basis of two theorems that provide conditions for non-planarity : • Theorem 1: If v ≥ 3 then e ≤ 3 v − 6; • Theorem 2: If v > 3 and there are no cycles of length 3, then e ≤ 2 v − 4. • If either of these theorems fail to hold then the graph is not planar.
Euler’s formula • The graph K 3, 3 has 6 vertices, 9 edges, and no cycles of length 3. Using Theorem 2 we get the implication. • If (v > 3) and not(cycle(length(3)) then e ≤ 2 v − 4. • RHS = 9 ≤ (2*6) – 4 = 9 ≤ 8 = false • The LHS of the implication is true, but RHS is false (T=>F is false 1). • By theorem 2, K 3, 3 cannot be planar.
Satisfies Euler’s formula but not planar. • Consider a graph where the largest degree is greater than or equal to the number of vertices in the graph. For example, a graph on six vertices with five degree three vertices and one degree nine vertex. No such simple planar graph exists yet these numbers are consistent with the Euler Formula.
Planar Example • Euler Characteristic: r = e – v + 2, (4 = 8 – 6 + 2) (true) • r = regions or faces a b C f B e D A d c • For all planar graphs (e 3 v − 6), 8 > 12 = true • Note, this test cannot be used to prove planarity.
Planar Example
Planar embedding for K 2, 2, K 2, 3 , & K 2, 4
Kuratowski’s theorem • • Kuratowski’s Theorem: A graph is non-planar if and only if it contains a subgraph homeomorephic to K 3, 3 or K 5. The graph G on the left contain K, 3, 3. Deleting vertex h and its incident edges gives the middle graph. We can replace c-g-f by c-f (homeomorphic graphs). Then the graph on the left is K 3, 3 therefore G contains a subgraph that is homeomorphic to K 3, 3. (Page 447, course text)
Theorem on vertices, edges, regions • If the planar representation divides the plane then, v – e + r = 2 • If v≥ 3 then, e ≤ 3 v – 6 • If v≥ 3 and there are no cycles of length 3 then, e ≤ 2 v – 4
Proof that K 5 is not planar • A planar graph is a graph that can be drawn on the plane in such a way that its edges intersect only at their endpoints. • Solution: K 5 has 5 vertices each with degree 4, so by the handshaking theorem it has 5*4/2 = 10 edges. By theorem 1 of Euler’s formula, every connected planar graph must satisfy E ≤ 3 V – 6, gives 10 ≤ 3*5 – 6 or 10 ≤ 9, which is false. So K 5 cannot be a connected planar graph. • Or more simply, if K 5 was planar then m ≤ 15 - 6 = 9, but this is not the case because m = 10
Trees • A tree is a loop-free connected graph that contains no cycles. • Each pair of vertices of a tree is connected by a unique simple path. A Tree Not a tree
Trees • A tree with at least two vertices has at least two vertices of degree 1. • For every tree
Trees • Let G=
Spanning Tree Example A spanning tree of a connected graph is a tree that is a sub-graph and that contains all the vertices of the graph. In the middle below is a graph; on either side of it is a spanning tree of that graph. A spanning tree of a graph contains the minimum number of edges to keep the graph connected.
Spanning Tree Example An algorithm used to construct a spanning tree is non-deterministic in two ways: 1) the choice of vertex u is arbitrary, 2) the choice of the edge incident on u to add to the tree is arbitrary. A spanning tree of a graph contains the minimum number of edges to keep the graph connected.
Spanning Tree Example We can limit the choice of vertex by either breath first search (BFS) or depth first search (DFS). BFS Original Graph 1 3 6 DFS 1 2 5 4 7 2 4 3 7 5 The edges are traversed in the order specified by the numbers above. A spanning tree of a graph contains the minimum number of edges to keep the graph connected. 6
Shortest path
Permanent Labels Temporary Labels V the set of vertices in the graph, xpaths are next step F set of vertices whose shortest-path lengths remain to be calculated V – F (permanent labels) set of vertices whose shortest-path lengths have been calculated Each iteration moves one vertex from F to (V – F) Invariant : PF : (V – F) b ∊ (V – F)
Dijkstra’s Shortest Path Algorithm • Dijkstra’s algorithm for finding the shortest from a to v, involves assigning labels to vertices. Let L(v) denote the label of vertex v. At any point, some vertices have temporary labels and the rest have permanent labels, these are shown in red in the following slides. We let T denote the set of vertices having temporary labels.
Dijkstra’s Shortest Path Algorithm • If L(v) is a permanent label of v the L(v) is the shortest path to v. • Initially all vertices have temporary labels. • Each iteration of the algorithm changes the status of one of the labels from temporary to permanent. • Terminate when z is not an element of T.
Dijkstra’s Shortest Path Algorithm • • Initialize: L(a): = 0 all vn (except a) are labelled . T : = set of all temporary vertices while z ∊ T [ choose v ∊ T with min L(v) T : = T – {v} for each x ∊ T adjacent to v [ L(x) : = min{(L(x), L(v) + w(v, x)}]] Where w(v, x) is distance from v to x.
Example Graph G(*) 2 b c 2 1 2 a 3 4 4 d 1 f e 6 7 3 5 z g
Initialized Graph G(*) 2 b c 2 1 0 4 2 a 3 e 4 d 1 6 7 3 f z 5 g
After First Iteration Graph G(*) For each x in T adjacent to v: L(x) = min{Lold(x), L(v)+w(v, x)} 2 b c 2 2 1 Labels L(b) = min{ , 0+2} L(f) = min{ , 0+1} d f Chose (v= f ) ∊ T as minimum L(v) T : = set of all temporary vertices z e 4 6 7 3 1 3 4 2 a 1 Where w(v, x) is distance from v to x. 5 g
After Second Iteration Graph G(*) For each x in T adjacent to v: L(x) = min{Lold(x), L(v)+w(v, x)} 2 c 2 b 2 1 4 d f 6 7 5 z e 4 3 1 3 4 2 a Labels L(b) = min{ , 0+2} 1 L(f) = min{ , 0+1} L(d) = min{ , 0+1+3} L(g) = min{ , 0+1+5} Where w(v, x) is distance from v to x. g 6 Chose (v= b ) ∊ T as minimum L(v) T : = set of all temporary vertices
After Third Iteration(*) For each x in T adjacent to v: L(x) = min{Lold(x), L(v)+w(v, x)} 2 T : = set of all temporary vertices c 4 2 b 2 1 Labels L(b) = min{ , 0+2} 1 4 L(f) = min{ , 0+1} L(d) = min{ , 0+1+3} L(g) = min{ , 0+1+5} L(c) = min{ , 0+2+2} L(d) = min{4, 0+2+2} --dif. path L(e) = min{ , 0+2+4} 3 4 2 a Where w(v, x) is distance from v to x. 6 e 4 d f 1 6 7 3 5 z g 6 Chose (v= c ) ∊ T as minimum L(v)
Fourth Iteration(*) For each x in T adjacent to v: L(x) = min{Lold(x), L(v)+w(v, x)} 2 T : = set of all temporary vertices c 4 2 b 2 a Labels L(b) = min{ , 0+2} 1 L(f) = min{ , 0+1} 4 L(d) = min{ , 0+1+3} L(g) = min{ , 0+1+5} L(c) = min{ , 0+2+2} L(d) = min{4, 0+2+2} dif. Path L(e) = min{ , 0+2+4} L(e) = min{6, 0+2+2+3} dif. Path L(z) = min{ , 0+2+2+1} Where w(v, x) is distance from v to x. 1 4 4 2 d 3 6 e 6 7 3 f 1 5 z g 6 Chose (v= d ) ∊ T as minimum L(v) 5
Fourth Iteration(*) For each x in T adjacent to v: L(x) = min{Lold(x), L(v)+w(v, x)} 2 T : = set of all temporary vertices b c 4 2 2 a Where w(v, x) is distance from v to x. 1 2 4 3 Labels 6 L(b) = min{ , 0+2} 4 1 L(f) = min{ , 0+1} 4 d e L(d) = min{ , 0+1+3} L(g) = min{ , 0+1+5} 6 7 L(c) = min{ , 0+2+2} 3 L(d) = min{4, 0+2+2} dif. Path L(e) = min{ , 0+2+4} L(e) = min{6, 0+2+2+3} dif. Path f g 6 5 L(z) = min{ , 0+2+2+1} 1 L(e) = min{6, 0+2+2+4} b, f already min Chose (v= z ) ∊ T as minimum L(v) z 5
Fourth Iteration(*) For each x in T adjacent to v: L(x) = min{Lold(x), L(v)+w(v, x)} 2 T : = set of all temporary vertices b c 4 2 2 a Where w(v, x) is distance from v to x. 1 2 3 4 Labels L(b) = min{ , 0+2} 4 1 L(f) = min{ , 0+1} 4 d L(d) = min{ , 0+1+3} L(g) = min{ , 0+1+5} L(c) = min{ , 0+2+2} 3 L(d) = min{4, 0+2+2} dif. Path L(e) = min{ , 0+2+4} L(e) = min{6, 0+2+2+3} dif. Path f 5 L(z) = min{ , 0+2+2+1} 1 L(e) = min{6, 0+2+2+4} b, f already min Terminate z T z 6 e 6 7 g 6 5
Another Example: 1 b a. 2 c 3 2 0 2 1 a z 1 Labels L(b) = min{ , 0+2} L(d) = min{ , 0+1} 2 d a, 1 1 e Choose v ∊ T with minimum L(v)=d
Another Example: 2 b a, 2 c 3 2 0 2 1 a z 1 Labels L(b) = min{ , 0+2} L(d) = min{ , 0+1} L(e)=min{ , 0+1+1} 2 d a. 1 1 e d. 2 Choose v ∊ T with minimum L(v)=d
Another Example: 3 b a, 2 3 c b, 5 2 0 2 1 a z 1 Labels L(b) = min{ , 0+2} L(d) = min{ , 0+1} L(e)=min{ , 0+1+1} L(c)=min{ , 0+2+3} L(e)=min{2, 0+2+1} diff path 2 d a. 1 1 e d. 2 Choose v ∊ T with minimum L(v) =e
Another Example: 4 b a, 2 3 c b, 5 2 0 2 1 a z e, 4 1 Labels L(b) = min{ , 0+2} L(d) = min{ , 0+1} L(e)=min{ , 0+1+1} L(c)=min{ , 0+2+3} L(e)=min{2, 0+2+1} diff path L(z)=min{ , 0+1+1+2} Each node is labelled with its source node and the distance from the start node 2 d 1 e a. 1 d. 2 Shortest path: a, d, e, z Choose v ∊ T with minimum L(v)=z
Find shortest path a-f 7 d b 2 10 5 2 4 3 e a 6 7 c f
Find shortest path a-f 7 d b 2 10 5 2 4 3 a e a(0, a) 6 7 c f
Find shortest path a-f 7 d b 2 10 5 2 4 3 e a(0, a) a 6 7 c f Labels L(b) = min{ , 0+5} L(c) = min{ , 0+6} Each node is labelled with its source node and the distance from the start node
Find shortest path a-f 7 d b 2 10 5 2 4 3 e a(0, a) a 6 7 c f Labels L(b) = min{ , 0+5} L(c) = min{ , 0+6} L(d) = min{ , 0+5+7} L(f) = min{ , 0+5+10} L(e) = min{ , 0+5+2} L(c) = min{ , 0+5+3} Each node is labelled with its source node and the distance from the start node
Find shortest path a-f 7 d b 2 10 5 2 4 3 e a(0, a) a 6 7 c f Labels L(b) = min{ , 0+5} L(c) = min{ , 0+6} L(d) = min{ , 0+5+7} L(f) = min{ , 0+5+10} L(e) = min{ , 0+5+2} L(c) = min{ , 0+5+3} Each node is labelled with its source node and the distance from the start node
Find shortest path a-f 7 d b 2 10 5 2 4 3 e a(0, a) a 6 7 c f Labels L(b) = min{ , 0+5} L(c) = min{ , 0+6} L(d) = min{ , 0+5+7} L(f) = min{ , 0+5+10} L(e) = min{ , 0+5+2} L(c) = min{ , 0+5+3} L(e) = min{ , 0+6+7} Each node is labelled with its source node and the distance from the start node
Find shortest path a-f 7 d b Minimum 2 Paths 10 5 2 4 3 a f e a(0, a) 6 7 c Shortest path: a, b, e, f Length of path =11 Labels L(b) = min{ , 0+5} –from a L(c) = min{ , 0+6} –from a L(d) = min{ , 0+5+7} –from b L(f) = min{ , 0+5+10} – from b L(e) = min{ , 0+5+2} –from b L(c) = min{6, 0+5+3} –from b L(c) = min{6, 0+5+2 +7} –from e L(f) = min{ , 0+5+2 +4} – from e d not explored because (5+2+4)<12
Shortest Path Example
Shortest Path Example v 2 v 3 5 3 0 6 3 1 v 1 2 v 6 1 6 v 4 4 3 v 5
v 2 v 3 5 3 6 3 0 1 v 1 2 1 6 Step Vertex to be marked v 6 3 4 v 5 Distance to vertex Marked vertices v 1 v 2 v 3 v 4 v 5 v 6 0 v 1 0 3 6 1 v 2 0 3 , 8 6, 4 , 6 2 v 4 0 3 8, 5 4 6, 8 v 1, v 2, v 4 3 v 3 0 3 5 4 6, 8 , 11 v 1, v 2, v 4, v 3 4 v 5 0 3 5 4 6 11, 9 v 1, v 2, v 4, v 3, v 5 5 v 6 0 3 5 4 6 9 v 1, v 2, v 4, v 3, v 5, v 6 v 1, v 2
v 2 v 3 5 3 6 3 0 1 v 1 2 1 6 Step Vertex to be marked v 6 3 4 v 5 Distance to vertex Marked vertices v 1 v 2 v 3 v 4 v 5 v 6 0 v 1 0 3 6 1 v 2 0 3 , 8 6, 4 , 6 2 v 4 0 3 8, 5 4 6, 8 v 1, v 2, v 4 3 v 3 0 3 5 4 6, 8 , 11 v 1, v 2, v 4, v 3 4 v 5 0 3 5 4 6 11, 9 v 1, v 2, v 4, v 3, v 5 5 v 6 0 3 5 4 6 9 v 1, v 2, v 4, v 3, v 5, v 6 vi v 1, v 2
Find path a to f
UML for Dijkstra
Example of Rewriting Logic 1 mod VENDING-MACHINE { A Petri net of the vending machine [ Coin Item < Place < Marking ] op null : -> Marking -- empty marking ops $ q : -> Coin ops a c : -> Item var M : Marking op _ _ : Marking -> Marking {assoc comm id: null} rl [add-q] : M => M q. rl [add-$] : M => M $. rl [buy-c] : $ => c. rl [buy-a] : $ => a q. rl [change] : q q => $. }
Example of Rewriting Logic -- Show one way to spend 3 dollars ($ $ $) exec $ $ $. -- Show all ways to spend 3 dollars ($ $ $) red $ $ $ =(*)=> m: Marking. -- How can I get 2 apples with 3 dollars? red $ $ $ =(*)=> a a M: Marking.
Example of Rewriting Logic If I have a dollar and three quarters, can I get a cake and an apple? Yes. red $ q q q =(*, *)=>! a c M: Marking.
Transitions
River Puzzle 1 mod! RIVER-CROSSING { [ Side Group ] ops west east : -> Side op change : Side -> Side ops farmer wolf goat cabbage : Side -> Group op __ : Group -> Group { assoc comm } ops initial final : -> Group vars S S' : Side eq change(west) = east. eq change(east) = west.
River Puzzle 1 -- constraint for goat eaten ceq wolf(S) goat(S) farmer(S') = wolf(S) farmer(S') if S =/= S'. -- constraint for cabbage eaten ceq cabbage(S) goat(S) wolf(S') farmer(S') = goat(S) wolf(S') farmer(S') if S =/= S'. -- initial configuration all elements on the west. eq initial = farmer(west) wolf(west) goat(west) cabbage(west). -- final configuration all elements on the east eq final = wolf(east) farmer(east) goat(east) cabbage(east).
River Puzzle 1 trans [farmer-alone] : farmer(S) => farmer(change(S)). trans [wolf] : farmer(S) wolf(S) => farmer(change(S)) wolf(change(S)). trans [goat] : farmer(S) goat(S) => farmer(change(S)) goat(change(S)). trans [cabbage] : farmer(S) cabbage(S) => farmer(change(S))cabbage(change(S)). } open RIVER-CROSSING -- Evaluate using the rewrite predicate =(*)=> red initial =(*)=> wolf(east) farmer(east) goat(east) cabbage(east).
-- The following command will show the states show path 27 Starts with Farmer, Wolf, Goat, Cabbage on all West (W) bank [state 1] farmer(west) wolf(west) goat(west) cabbage(west) [state 2] farmer(east) wolf(west) goat(east) cabbage(west) [state 3] farmer(west) wolf(west) goat(east) cabbage(west) [state 4] farmer(east) wolf(east) goat(east) cabbage(west) [state 5] farmer(west) wolf(east) goat(west) cabbage(west) [state 6] farmer(east) wolf(east) goat(west) cabbage(east) [state 7] farmer(west) wolf(east) goat(west) cabbage(east) [state 8] farmer(east) wolf(east) goat(east) cabbage(east)
Farmer, Wolf, Goat, Cabbage on all West (W) bank River-Crossing
River-Crossing possible states & safe states Safe states
A directed graph or digraph g 1 c e a b d Fig. 19. 1
Sample Graph g 2 v 1 Graph g 2 v 4 v 2 v 5 v 3
Sample Graph g 3 v 6 Graph g 2 v 1 v 5 v 2 v 4 v 7
[('a', 'c', 2), ('a', 'd', 6), ('b', 'a', 3) , ('b', 'd', 8), ('c', 'd', 7), ('c', 'e', 5) , ('d', 'e', 10)] Sample Graph g 3 8 b d 10 6 3 2 a build. Graph [('a', 'c', 2), ('a', 'd', 6), ('b', 'a', 3) , ('b', 'd', 8), ('c', 'd', 7), ('c', 'e', 5) , ('d', 'e', 10)] 7 c 5 e from. List [('a', [('b', 3. 0), ('d', 6. 0), ('c', 2. 0)]), ('b', [('d', 8. 0), ('a', 3. 0)]), ('c', [('e', 5. 0), ('d', 7. 0), ('a', 2. 0)]), ('d', [('e', 10. 0), ('c', 7. 0), ('b', 8. 0), ('a', 6. 0)]), ('e', [('d', 10. 0), ('c', 5. 0)]) ] build graphs makes edges bidirectional
Review • Definitions for path, simple path, degree of vertex. • Understand real world examples where these concept could be used. • Find path, simple path, cycle, or simple cycle.
Review • Handshaking Theorem • Holds for a particular graph
Review • Showing Euler Cycle exists in a graph • Finding an Euler Cycle in a particular graph. • Finding a Hamilton Cycle in a particular graph.
Review • Definitions complete graph and planar graph. • Prove that the complete graph K 5 is not planar.
Review • What is a bipartite graph. • The complete K 3 is not bipartite. • Shortest path a-f
Shortest Path in C# • A=0(A) B=2(A) C=3(D) D=1(A) E=3(D) F=6(G) G=5(D)
x 2 B C 2 x 1 2 A 1 4 D F 5 Z E 6 7 3 x 3 4 G