0% found this document useful (0 votes)
33 views

Exercise Sheet 10

The document contains 4 exercises: 1) Lists BFS and DFS search trees for a sample graph 2) Describes Dijkstra's algorithm for finding the shortest path between nodes in a graph 3) Provides a topological sort algorithm to order nodes in a graph with no cycles 4) Proves that a graph with n nodes and O(n) edges cannot contain a cycle of 4 nodes

Uploaded by

danik2002z
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Exercise Sheet 10

The document contains 4 exercises: 1) Lists BFS and DFS search trees for a sample graph 2) Describes Dijkstra's algorithm for finding the shortest path between nodes in a graph 3) Provides a topological sort algorithm to order nodes in a graph with no cycles 4) Proves that a graph with n nodes and O(n) edges cannot contain a cycle of 4 nodes

Uploaded by

danik2002z
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exercise Sheet 10

Exercise 1:

a
BFS: 7 4 6 9 10 1 2 3 8 5
Search tree:

b
DFS: 7 4 1 2 3 8 5 6 9 10
Search tree:
Exercise 2:

Iteration D 1 D 2 D 3 D 4 D 5 D 6 u
0 0 ∞ ∞ ∞ ∞ ∞ 1
1 0 1 ∞ ∞ ∞ ∞ 2
2 0 1 ∞ 1 ∞ ∞ 4
3 0 1 2 1 ∞ ∞ 3
4 0 1 2 1 2 ∞ 5
5 0 1 2 1 2 3 6
Exercise 3:
function topologicalSort graph :
visited // Dictionary to keep track of visited nodes
ordering // List to store the topological ordering

for each node in graph:


if node is not visited:
dfs node, visited, ordering

return reverse ordering // Return the reversed ordering to obtain the correct topological sorting

function dfs node, visited, ordering :


visited node true

for each adjacentNode in node.adjacentNodes:


if adjacentNode is not visited:
dfs adjacentNode, visited, ordering

ordering.append node // Append the current node to the ordering


Exercise 4:
𝑥 𝑥 𝑥 ⋯ 𝑥 𝑓 𝑥 𝑓 𝑥 ⋯ 𝑓 𝑥 1
𝑓 𝑥 ⇒ 𝑓 ⇒ 𝑓 𝑑 𝑣
2 𝑘 𝑘 𝑛 ∈
1 1 1 𝑑 𝑣 2
𝑓 𝑑 𝑣 ⇒ 𝑓 𝑑 𝑣 |𝐸 𝐺 |
𝑛 ∈ 𝑛 ∈ 𝑛 ∈ 2 𝑛
Assume, that G does not contain a cycle with 4 nodes 𝐶 . In such case, every subgraph induced by4 vertices must be
4
acyclic. Thus, the maximum number of edges that can be added by fixing any 4 vertices is given by 6⇒
2
|𝐸 𝐺 | 6 ⇒ |𝐸 𝐺 | 3𝑛. Therefore we shown that the graph G can have at most 𝑂 𝑛 edges without containing
a cycle with 4 nodes 𝐶 .

You might also like