While trying to come up with one of his stupid Graph Theory problems, Agnishom wrote the following function to generate a random graph:
1 2 3 4 5 6 7 |
|
Chris told Agnishom that this algorithm is not really that great and can generate only a particular class of graphs.
Which of the following is the best way to describe these class?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Function genGraph ( n ) generates a graph on the integer-labeled nodes { 1 , . . . , n } as follows: Starting with node v = 2 and an empty edge list, each node v ∈ { 2 , . . . , n } is selected in sequence and one node u is randomly selected from among the integers less than v , i.e., u ∈ { 1 , . . . , v − 1 } , to form edge e = ( v , u ) , which is then added to the edge list of the graph. Therefore, one edge is generated for each value v considered, so the total number of edges generated is ∣ { 2 , . . . , n } ∣ = n − 1 .
Furthermore, the resulting graph is connected. To see this, proceed inductively on the generated subgraph of the first v nodes { 1 , . . . , v } .
In the first iteration for nodes { 1 , 2 } , v = 2 and u is randomly selected from { 1 , . . . , v − 1 } = { 1 } . Therefore, edge ( 2 , 1 ) is added to edgeList ⇒ the generated subgraph with nodes { 1 , 2 } is connected.
In the iteration for nodes { 1 , . . . , v } , v ≤ n , new edge ( v , u ) is created by randomly choosing a node u in { 1 , . . . , v − 1 } . But by the induction hypothesis, the generated subgraph { 1 , . . . , v − 1 } is connected. Therefore, v is adjacent to a node in a connected subgraph ⇒ the subgraph { 1 , . . . , v } is connected.
So, function genGraph ( n ) generates a connected graph with n nodes and n − 1 edges. Therefore, the graph must be a tree .