Name: 100 points total CSE 3353 Homework 2 Spring 2015 Assignment is due at 11am on February 19. Hand in the written portion during class that day; if that is not feasible, hand it in to Debra McDowell, CSE Dept Secretary, on the 4th floor of Caruth Hall. Turn in a copy of the code and outputs in a .zip file on Blackboard. The Python file for Q1 must be named traverse.py, and you must not modify the names of any of the given functions or their defined parameters. You may add your own functions as you see fit. Any outputs from running the code should be included in files called traverse output.txt, also included in the zip file. Both of these files must be included in a directory called LastnameFirstname. Please write your name(s) as a comment in the first line of code in the python files and output files. You may submit a lateness coupon request BEFORE the assignment is due by sending a private Piazza message with Subject “CSE3353 Lateness Coupon”. All other late work will not be accepted. Q1 (50) Q2 (8) Q3 (8) Q4 (8) Q5 (9) Q6 (15) Q7 (2) Total (100) 1 Programming Q1. (50 points) In this problem you will write code to efficiently crawl webpages, as well as answer questions about the links between webpages. Consider the following example website: http://lyle.smu.edu/˜mvasek/courses/ cse3353/webtraverse/index.html. This website has only internal links to other pages on the same website, which makes it ideal for testing. Starter code is available from http://lyle.smu.edu/˜mvasek/courses/cse3353/code/ traverse_starter.txt. You may use the helper function getLinks(url,baseurl), which fetches a given URL and extracts all links, returning them as a list of URLs. Note: you must install the Python package BeautifulSoup, which parses HTML code. See http://lyle.smu.edu/˜mvasek/courses/cse3353/ pypackages.html for instructions. You should create a dictionary object mapping a URL to a list of links contained in that URL. For example: G[’http://lyle.smu.edu/˜mvasek/courses/cse3353/webtraverse/index.html’]= [’http://lyle.smu.edu/˜mvasek/courses/cse3353/webtraverse/alink.html’, ’http://lyle.smu.edu/˜mvasek/courses/cse3353/webtraverse/blink.html’, ’http://lyle.smu.edu/˜mvasek/courses/cse3353/webtraverse/clink.html’, ’http://lyle.smu.edu/˜mvasek/courses/cse3353/webtraverse/index.html’] You are strongly encouraged to write additional functions that can be called from the required functions in the question. Include output for all the function calls from within the if name ==" main ": block of the starter code. a. Implement print dfs(url): print all links reachable from a starting URL url in depth-first order. b. Implement print bfs(url): print all links reachable from a starting URL url in breadth-first order. c. Implement find shortest path(url1,url2): find and return the shortest path from url1 to url2 if one exists. d. Implement find max depth(url): find and return the URL that is the greatest distance from url, along with the sequence of links that must be followed to reach the page. For this problem, distance is defined as the minimum number of links that must be followed to reach the page. 2 Graph Algorithms Q2. (8 points) Consider the following directed graph: a b c e d f g a. Construct an adjacency list. b. Construct an adjacency matrix. c. Are there any cycles in this graph? If so, please list them. 3 Q3. (8 points) Consider the following undirected graph: a b c d h g e f Construct a BFS tree for a breadth-first traversal beginning at node a. Break ties by picking the vertex that comes first alphabetically. Be sure to number each edge in the order it was added, and to include cross edges. Also, include a list of nodes in the order they are discovered, plus a list of nodes in the order they are finished processing. 4 Q4. (8 points) Consider the following undirected graph: a b c d h g e f Construct a DFS tree for a depth-first traversal beginning at node a. Break all ties by picking the vertex that comes first alphabetically. Be sure to number each edge in the order it was added, and to include back edges. Also, include a list of nodes in the order they are discovered, plus a list of nodes in the order they are finished processing. 5 Q5. (9 points) Consider the following directed acyclic graph: a d e c b f Identify a topological sorting of vertices of the above graph. a. Construct a depth-first search tree starting at node a. Break all ties by picking the vertex that comes first alphabetically. Be sure to number each edge in the order it was added, and to include back, cross or forward edges. Also, include a list of nodes in the order they are discovered, plus a list of nodes in the order they are finished processing. b. Based on your answer to part (a), give a topological sorting of the vertices in the graph. 6 Q6. (15 points) a. In class, we showed that every tree with n nodes has n − 1 edges. Is every graph with n nodes and n − 1 edges necessarily a tree? Prove or give a counter example. b. Prove that every graph has an even number of vertices of odd degree. (Hint: direct proof using graph properties proved in class) c. A binary tree is a rooted tree in which each node has at most two children. Show by induction that in any binary tree, the number of nodes with two children is exactly one less than the number of leaves. Q7. (2 points) How long (in hours) did you spend on this assignment? Please estimate separately how long you spent on programming (Q1) and the other questions (Q2–Q6) (full credit for any truthful answer) 7
© Copyright 2024