CSE 3500 — Algorithms and Complexity — Yufeng Wu — Spring 2016 Homework 3 ——- Due: 02/10/16 at the end of the day. 1 Manually run the topological sort algorithm Run the DFS-based topological sort algorithm taught in class (not the algorithm given in the textbook, which removes an in-degree zero node each time) using the example given in Figure 3.8 part (a) (page 103) of the textbook (for your convenience, also provided here; ignore the shades of the nodes). When performing DFS, when there are multiple choices, use the alphabetical order. For example, you should begin with v1 (the alphabetically smallest). From v1 , you should first visit v4 . Note: you should show the DFS timestamps and the order of the output nodes. 2 Cycles in undirected graph This is similar to Problem 3.2 (on p.107) of the textbook. Briefly, give an algorithm that determines whether or not a given undirected graph G = (V, E) contains a cycle. Note: I am not asking you to output a cycle; you just need to determine whether G contains a cycle or not. And your algorithm should run in O(|V |) time, independent of |E|. 3 A distance problem Do problem 3.9 on page 110. For your convenience, here is the problem description. Suppose a n-node undirected (and unweighted) graph G = (V, E) contains two nodes s and t such that the distance between s and t is strictly greater than n2 . Show that there must exist some node v (different from s and t) such that deleting v (and all edges incident to v) from G will destroy all paths between s and t. In other words, the new graph by deleting v from G contains no path between s and t. Given an algorithm with running time O(m + n) to find such v (m is the number of edges). Hint: since this problem concerns distance in a unweighted graph, this leads naturally to BFS... 4 Querying on a tree You are given a rooted binary tree T = (V, E), along with a designated root node r ∈ V . The size of V is n. Recall that a node in a tree keeps tracks of its descendants and its parent node. Also recall 1 that node u is said to be an ancestor of node v in the rooted tree, if the path from r to v in T passes through u. A commonly performed querying on the trees is: given two nodes u and v, is u an ancestor of v? You wish to preprocess the tree so that queries of this form for any two nodes u and v can be answered in constant time. The preprocessing itself should take linear time. That is, you can spend O(n) time before any query arrives; then you must answer each query like “is node u an ancestor of node v?” in constant time. 10% extra credits: paths on a tree This problem is for students who are looking for some challenge. You are given T , a undirected tree (i.e. connected and acyclic graph). The objective is finding two nodes u, v of T s.t. the distance (i.e. the number of edges along the unique path between u and v) is the maximum. Naı̈vely, you can try all possible pairs of nodes but this is very slow. Here is a very simple algorithm for this problem that runs in linear time. You start from any node s as the source and perform BFS. Let u be the node with the largest distance from s (i.e. at the largest level). Then you perform a second BFS: this time you use u as the source. Let v be the node with the largest distance from u. Now, prove this claim: the distance between u and v is the largest possible distance between any two nodes on the tree. 2
© Copyright 2026 Paperzz