Download Q 9.1 Find a topological ordering for the graph in Figure 9.79

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Computational complexity theory wikipedia , lookup

Computational electromagnetics wikipedia , lookup

Clique problem wikipedia , lookup

Signal-flow graph wikipedia , lookup

Centrality wikipedia , lookup

Graph coloring wikipedia , lookup

Travelling salesman problem wikipedia , lookup

Transcript
Q 9.1
Find a topological ordering for the graph in Figure 9.79.
Solution.
Note that the solution is not unique. That is there is more than one topological
ordering for this graph. The following topological ordering results from using a
queue: s, G, D, H, A, B, E, I, F, C, t
Q 9.2
If a stack is used instead of a queue for the topological sort algorithm in Section 9.1, does a different ordering result? Why might one data structure give a
“better” answer?
Solution. Yes. Using a queue will cause the nodes of the graph to be traversed
in a BFS manner. This is a more “natural” ordering nodes that are the same
distance from the source will appear consecutively.
Q 9.4
An adjacency matrix requires O(|V 2 |) merely to initialize using the standard
“two nested for loops” idiom. Propose a method that stores a graph in an
adjacency matrix (so that testing for the existence of an edge is O(1)) but avoids
the quadratic running time.
Solution. Let G = (V, E) be a graph. Label the vertices 1, ..., |V |. This labeling
induces an ordering on the set of neighbours of a vertex. Let pos(i, j) denote the
position of vertex j in this induced ordering of the neighbours of i. Create two
|V | × |V | matrices called POSITION and VERTEX-IN-POSITION and a |V |
vector called DEG as follows:
– POSITION(i,j) = pos(i,j), for every edge (i, j) ∈ E;
– VERTEX-IN-POSITION(i, POSITION(i,j)) = j, for every edge (i, j) ∈ E;
– DEG(i) = the degree of vertex i for every edge i ∈ V .
This representation allows for the testing of edge existence in O(1)-time. It
also allows one to iterate over all the edges incident on a vertex v in O(degree(v))time. The number of locations in the matrices actually initialized equals O(|V |+
|E|) though.
Note that this representation was introduced by Gabriel Valiente, in a paper
called “Trading uninitialized space for time” in May of 2004; see resource matrix
for full paper.
Question 9.5
a. Find the shortest weighted path from A to all other vertices for the graph in
Figure 9.80.
b. Find the shortest unweighted path from B to all other vertices for the graph
in Figure 9.80.
1
B
5
G
3
2
3
A
C
7
E
2
7
2
1
1
6
D
F
Figure. 9.80
Fig. 1.
Solution.
a. This is an instance of the “WEIGHTED SINGLE-SOURCE SHORTEST
PATH PROBLEM”. The general method to solve this problem is known as
Dijkstra’s algorithm.
–
–
–
–
–
–
A→C
A→B
A→B
A→B
A→B
A→B
→G
→G→E
→G→E→F
→ G → E → D.
b. This is an instance of the “UNWEIGHTED SINGLE-SOURCE SHORTEST
PATH PROBLEM”. The problem can be solved using breadth first search.
–
–
–
–
–
–
B
B
B
B
B
B
→C
→E
→G
→C→D
→E→F
→ C → D → A.
Question 9.52 The object of the Kevin Bacon Game is to link a movie actor to
Kevin Bacon via shared movie roles. The minimum number of links is the actor’s
Bacon number. For instance, Tom Hanks has a Bacon number of 1; he was in
Apollo 13 with Kevin Bacon. Sally Fields has a Bacon number of 2, because she
was in Forest Gump with Tom Hanks. Assume that you have a comprehensive
list of actors, with roles, and do the following:
a. Explain how to find an actors Bacon number.
b. Explain how to find the actor with the highest Bacon number.
c. Explain how to find the minimum number of links between two arbitrary
actors.
Solution.
a. Build a graph with a vertex corresponding to each actor such that there is an
edge between two actors if and only if they were in the same movie. To find an
actors Bacon number is simply a matter of finding the shortest path between the
vertex corresponding to the actor and the vertex corresponding to Kevin Bacon
in this graph.
b. Find the shortest path between the vertex corresponding to Kevin Bacon and
every other vertex. The vertex with the longest “shortest path” to it corresponds
to the actor with the highest Kevin Bacon number.
c. Find the shortest path between them.