Section 13
description
Transcript of Section 13
![Page 1: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/1.jpg)
Section 13
Questions
Questions
Graphs
![Page 2: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/2.jpg)
Graphs
A graph representation:
– Adjacency matrix.• Pros:?
• Cons:?
– Neighbor lists.• Pros:?
• Cons:?
![Page 3: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/3.jpg)
Graphs
A graph representation:
– Adjacency matrix.• Pros: O(1) check if u is a neighbor
of v.• Cons:Consumes lots of memory for
sparse graphs. Slow in finding all neighbors.
– Neighbor lists.• Pros:Consume little memory, easy to
find all neighbors. • Cons:Check if u is a neighbor of v
can be expensive.
![Page 4: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/4.jpg)
Graphs
In practice, usually neighbor lists are used.
![Page 5: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/5.jpg)
Some graphs we often meet.
Acyclic - no cycles.Directed, undirected.Directed, acyclic graph = dag.
![Page 6: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/6.jpg)
Topological sorting
A problem: Given a dag G determine the ordering of the vertices such that v precedes u iff there is an edge (v, u) in G.
Algorithm: Take out the vertice with no incoming edges along with its edges, put in the beginning of a list, repeat while necessary.
Implementation?
![Page 7: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/7.jpg)
Topological sorting
More descriptive description:1. For each vertice v compute
count[v] - the number of incoming edges.
2. Put all the vertices with count[v] = 0 to the stack
3. repeat: Take top element v from the stack, decrease count[u] for its neighbors u.
![Page 8: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/8.jpg)
Topological sorting
Implementation:1. Use DFS/BFS to compute
count.
2. Complexity?O(E + V) - 1. takes E, the loop
is executed V times, the number of operations inside loop sums up to E.
![Page 9: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/9.jpg)
Topological sorting
Applications:– Job scheduling.
![Page 10: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/10.jpg)
The bridges of Konigsberg
![Page 11: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/11.jpg)
The bridges of Konigsberg
In 1735 Leonard Euler asked himself a question.
Is it possible to walk around Konigsberg walking through each bridge exactly once?
![Page 12: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/12.jpg)
The brid(g)es of Konigsberg
![Page 13: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/13.jpg)
The bridges of Konigsberg
It’s not possible!
It’s a graph problem!
![Page 14: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/14.jpg)
Euler’s theorem
The Euler circuit passes through each edge in the graph only once and returns to the starting point.
Theorem: The graph has an Euler circuit iff every vertice has even degree. (the number of adjacent vertices)
![Page 15: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/15.jpg)
Euler’s circuit
How to check if there’s one?
![Page 16: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/16.jpg)
Hamiltonian’s circuit
A cycle which passes through vertice only once.
How to find one efficiently?
![Page 17: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/17.jpg)
Hamiltonian’s cycle
Nobody knows!!!
![Page 18: Section 13](https://reader035.fdocuments.in/reader035/viewer/2022062808/56815214550346895dc052e4/html5/thumbnails/18.jpg)
REWARD!!!
$1000000is offered to anyone who finds an
efficient algorithm for finding Hamiltonian cycle in a graph.
DEAD OR ALIVE