Answer true the maximum number of edges in an

Info icon This 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
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
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 element in the adjacency list of w (or it’s nil if that list is empty) mark w as visited 5. (30 pts) Recall that a simple is a path in which no vertex is repeated. Give the pseudocode of an algorithm that, given a digraph G and a node
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern