Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
Graph Traversals
CSC 1300 – Discrete StructuresVillanova University
Villanova CSC 1300 - Dr Papalaskari 1
Major ThemesGraph traversals:• Eulerian circuit/trail
– Every edge exactly once
• Hamiltonian cycle– Every vertex exactly once– Shortest cycle:
• Traveling Salesman Problem – Shortest Path:• Dijkstra’s algorithm
8
2
574
3 2
8
59
Villanova CSC 1300 - Dr Papalaskari
2
The Bridges of Königsberg Puzzle18th century map of the city of Königsberg with 7 bridges over the Pregel river
Find a walk through the city that would cross each bridge once and only once before returning to the starting point
Villanova CSC 1300 - Dr Papalaskari 3
Bridges of Königsberg: Euler’s Solution
A
C
B
D
Leonhard Euler (1707-1783) rephrased the question in terms of a multigraph
C
A D
B
Is there a circuit that traverses all the edges exactly once?
Villanova CSC 1300 - Dr Papalaskari 4
Eulerian traversalsEulerian trail: Open trail that traverses all the edges Eulerian circuit: A circuit that traverses all the edges
Examples:
Villanova CSC 1300 - Dr Papalaskari 5
Next: Determining whether a graph or multigraph has an Eulerian circuit
Villanova CSC 1300 - Dr Papalaskari 6
Villanova CSC 1300 - Dr Papalaskari 7
Eulerian circuits: Necessary and sufficient conditions
Theorem: A connected multigraph G has an Eulerian circuit if and only if every vertex of G has even degree.
( è ) A necessary condition: G has an Eulerian circuit only if every vertex has even degree
Proof idea: Assume G has an Eulerian circuit. Observe that every time the circuit passes through a vertex, it contributes 2 to that vertex’s degree:• the circuit must enter via an edge incident with this vertex, and• leave via a different incident edge.
Villanova CSC 1300 - Dr Papalaskari 8
(ç ) A sufficient condition:G has an Eulerian circuit if every vertex has even degreeProof idea: Assume every vertex in a multigraph has even degree. • Start at an arbitrary vertex v0, choose an arbitrary edge (v0,v1)
• Choose an arbitrary unused edge from v1 and so on. Since the vertices have even degree, there is always an edge to choose.
• … After a finite number of steps the process will arrive at the starting vertex v0, yielding a circuit with distinct edges.
• (But we may not be done! The circuit may not yet include all edges of G)
• Start again at an arbitrary vertex with untraversed edges; repeat above process. This newer traversal can now be spliced into the original one.
• Continue in this manner until all the edges have been traversed, splicing any new traversals to create a Eulerian circuit Villanova CSC 1300 - Dr Papalaskari
9
Eulerian circuits: Necessary and sufficient conditions
1 3 5 7
2 4 6 8
1 3 5 7
2 4 6 8
Example
Gcircuit1231
Edges remaining1 3 5 7
2 4 6 8
1 3 5 7
2 4 6 81 3 5 7
2 4 6 8
circuit34653
splicing: 1 3 5 7
2 4 6 8
Villanova CSC 1300 - Dr Papalaskari 10
12346531
splicing6786
1 3 5 7
2 4 6 8
1 3 5 7
2 4 6 8
1 3 5 7
2 4 6 8
Splicing:12346786531 Eulerian circuit
1 3 5 7
2 4 6 8
Villanova CSC 1300 - Dr Papalaskari 11
circuit12346531
circuit6786
Example (continued)
G Edges remaining
Eulerian trails: Necessary and sufficient conditions
Villanova CSC 1300 - Dr Papalaskari 12
Theorem A connected graph G has an Eulerian trail if and only if it has exactly two vertices of odd degree.
Villanova CSC 1300 - Dr Papalaskari 13
Icosian GameIcosian Game This puzzle has been invented by the renowned Irish mathematician Sir William Hamilton (1805-1865) and presented to the world under the name of the Icosian Game. The game's board was a wooden board with holes representing major world cities with grooves representing connections between them. The object of the game was to find a circular route that would pass through all the cities exactly once before returning to the starting point.
Villanova CSC 1300 - Dr Papalaskari 14
Traveling Salesman Problemfind the lowest cost Hamilton cycle
Villanova CSC 1300 - Dr Papalaskari 15
Optimal tour of the 13,509 cities in the USA with populations greater than 500.
More about TSP: http://www.math.uwaterloo.ca/tsp/index.htmlImage:http://www.wired.com/images_blogs/wiredscience/2013/01/tsp_map.jpg
Hamiltonian Cycle?
K6C3 K4
Villanova CSC 1300 - Dr Papalaskari 16
Hamilton Cycle: Sufficient condition(but NOT necessary)
Let G be a graph with n vertices where n ³ 3. If the degree of every vertex is at least n/2 , then G has a Hamilton cycle.
Villanova CSC 1300 - Dr Papalaskari 17
Intuition: Graphs with lots of edges must have a Hamilton cycle
Hamilton Cycle: Necessary conditions(but NOT sufficient)
If a graph G has a Hamiltonian cycle…• k(G) = 1 (connected)• No vertices of degree 1• No cut vertices/edges• For every proper subset S of V(G),
k(G - S) ≤|S|
Villanova CSC 1300 - Dr Papalaskari 18
Intuition: In hamiltonian graphs, you can remove some vertices without disconnecting them into too many pieces (i.e., more than the number of vertices removed).
Theorem 12.57
Villanova CSC 1300 - Dr Papalaskari 19
• No simple necessary and sufficient conditions are known to verify existence of a Hamilton cycle for general graphs.
• This means that the problem has no known efficient solution.
Villanova CSC 1300 - Dr Papalaskari 20
Villanova CSC 1300 - Dr Papalaskari 21
Q0 Q1 Q2 Q3 Q4
Hypercube graphs
Hypercube graphs
Villanova CSC 1300 - Dr Papalaskari 22
Images by NerdBoy1392 - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=5514315Watchduck (a.k.a. Tilman Piesk) - Own work, Public Domain, https://commons.wikimedia.org/w/index.php?curid=2674082https://people.eecs.berkeley.edu/~jfc/cs174lecs/lec11/lec11.html
See also: https://en.wikipedia.org/wiki/Hypercube
Q0 Q1 Q2 Q3 Q4
Q4
Q3
Q4
https://commons.wikimedia.org/w/index.php%3Fcurid=5514315https://commons.wikimedia.org/w/index.php%3Fcurid=2674082https://people.eecs.berkeley.edu/~jfc/cs174lecs/lec11/lec11.htmlhttps://en.wikipedia.org/wiki/Hypercube
Find a Hamilton cycle
List the bitstrings, in order:1. __________2. __________3. __________4. __________5. __________6. __________7. __________8. __________9. __________
Villanova CSC 1300 - Dr Papalaskari 23
0 0 0
0 0 0
Find a Hamilton cycleList the bitstrings, in order:1. __________2. __________3. __________4. __________5. __________6. __________7. __________8. __________9. __________10. __________11. __________12. __________13. __________14. __________15. __________16. __________17. __________ (back to 1.)
Villanova CSC 1300 - Dr Papalaskari 24Image by Watchduck (a.k.a. Tilman Piesk) - Own work. This W3C-unspecified vector image was created with Inkscape., Public Domain, https://commons.wikimedia.org/w/index.php?curid=11402515
https://commons.wikimedia.org/w/index.php%3Fcurid=11402515
Gray codes
Devices indicate position by closing and opening switches or sensing and digitizing rotational angle. Decimal Binary... ...2 0103 0114 100... ...
Physical switches are not ideal and if they don’t switch state in synchrony, the reading may appear as 110.
Villanova CSC 1300 - Dr Papalaskari 25
Problem here: all three bits are changing!
Gray codes
If bits only change one at a time, we can not get large errors.Gray codes (also called “reflected binary code”) solve this problem by changing only one bit at a timeSee also: https://en.wikipedia.org/wiki/Gray_code
Villanova CSC 1300 - Dr Papalaskari 26Graphic by “Perlygatekeeper - Own work, CC BY-SA 4.0,” https://commons.wikimedia.org/w/index.php?curid=59774462
https://en.wikipedia.org/wiki/Gray_code