{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Answer true the maximum number of edges in an

Info iconThis preview shows pages 2–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Answer TRUE. The maximum number of edges in an undirected connected graphs without self-loops and without parallel edges with 100 nodes is ( 100 2 ) = 4950. Out of these, 100 - 1 = 99 are discovery edges (form the spanning tree). This leaves a maximum of 4851 = 4950 - 99 backedges. 2. (20 pts) Consider the undirected graph on eight nodes, labelled 0 through 7, with the following 13 edges: 0-1 0-6 0-7 1-4 1-6 1-7 2-3 2-4 2-5 3-4 3-6 3-7 5-6 Suppose we run the BFS algorithm on this graph, starting at node 0 and such that BFS explores the edges incident to a node in the numerical order of the labels of the node at the other end. (a) Draw the spanning tree of discovery edges produced by this algorithm. Answer This not required for the answer, but let’s list the nodes and edges in the order they are explored: L0: 0 0-1(disc) 0-6(disc) 0-7(disc) L1: 1 6 7 1-4(disc) 1-6(cross) 1-7(cross) 6-3(disc) 6-5(disc) 7-3(cross) L2: 4 3 5 4-2(disc) 4-3(cross) 3-2(cross) 5-2(cross) L3: 2 And the spanning tree is 1 --- 0 --- 7 | | | | 4 6 | |\ | | \ 2 3 5 2
Background image of page 2

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
(b) Give an example of a “cross edge” explored by the BFS algorithm. Answer Here are all of them: 1-6, 1-7, 7-3, 4-3, 3-2, 5-2 3. (20 points) Consider a graph G (undirected) and one of its vertices s . Let T be the spanning tree of discovery edges obtained by running DFS ( G,s ) [TYPO: replace DFS with BFS]. T can be considered as rooted tree with root s , therefore it has a height . Draw a graph G such that G has 7 nodes G has 3 cycles T has height 2 Answer For example: let 1 , 2 , 3 , 4 , 5 , 6 , 7 be the vertices, s be 1 and we’ll have 9 edges: 1-2 1-5 2-3 2-4 2-5 3-4 5-6 5-7 6-7 4. (30 points) Give the pseudocode of a a non-recursive implementation of DFS on digraphs. Use a stack. Answer Answer First look at the recursive algorithm and think about the information that is being savedd when the recursive called are made. This information includes the node v and an iterator object that iterates through the nodes in the adjacency list of v . When the recursive call return this iterator continues from where it was before the recursive call. When we code the algorithm recursively this information is saved in the recursive calls stack of the implemention. here we are going to have to manage this stack ourselves. Therefore we will use a stack S whose elements are pairs ( v,r ) where v is a node and r is a reference to an element in the adjacency list of v (or “nil”). The algorithm is as follows: initialize S to contain just (s,r0) where r0 is a reference to the first element in the adjacency list of s (or it’s nil if that list is empty) mark s as visited while S is nonempty do the following: pop S, call (v,r) what you get if r is not nil then initialize a reference p=r let w be the vertex in the adjacency list of v that p refers to while p is not nil and w is marked update p to the next element in the adjacency list of v (this could result in p becoming nil) if p is not nil (therefore w is unmarked) then update p to the next element in the adjacency list of v 3
Background image of page 3
(this could result in p becoming nil) push (v,p) onto S push (w,r0) onto S where r0 is a reference to the first
Background image of page 4

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page2 / 6

Answer TRUE The maximum number of edges in an undirected...

This preview shows document pages 2 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online