problem set 1

2
Introduction to Algorithms Problem Set 1 CS 4820 Spring 2015 Due 11:59pm Thursday, January 29 name: Jonathan Mares netid: jm2242 collaborators: None Your homework submissions need to be typeset (hand-drawn figures are OK). See the course web page for suggestions on typing formulas. Solution to each question needs to be uploaded to CMS as a separate pdf file. (Questions with multiple parts need to be uploaded as a single file.) Remember that when a problem asks you to design an algorithm, you must also prove the algorithm’s correctness and analyze its running time. The running time must be bounded by a polynomial function of the input size. Note: Due to delay in the textbook arriving to campus store, we posted two sections of the book on CMS that are the most helpful for the current homework. (1) Mark Lombardi (1951-2000) http://en.wikipedia.org/wiki/Mark_Lombardi was an Amer- ican neo-conceptual artist who specialized in drawings that use famous people’s social networks to document alleged financial and political frauds. Many of his famous drawings are graphs of social net- works of powerful individuals, connecting pairs of people with particular relationships. People then wonder what it means when certain pairs of people are connected in this graph by a surprisingly short path. Of course, a single short path can be accidental, maybe many short paths are more sure to mean strong social connection. The problem here is to give an efficient algorithm to find the number of shortest path between two points. Given an undirected graph G with n nodes and m edges, and two nodes of interest u and v. We are interested in shortest paths, i.e., paths with the minimum number of edges. Give an O(m + n) time algorithm to find the length of the shortest path, and the number of different shortest paths between u and v. Please note that you do not need to output the paths themselves, only the length of the shortest paths, and the number of different shortest paths between u and v. Solution. Algorithm I will assume that the nodes are labeled from 0 to n-1 if there are n nodes. To find the length of the shortest path, a breadth first search on the graph can be conducted. To find the number of different shortest paths between u and v, we modify the breadth first search algorithm. Beginning at node u, in addition to visiting each neighbor node, we label the visited node with the number of paths from u to that node. We can find the number of paths to that node by checking for every neighbor of the visited node j, if it has been visited, then we look up Paths[j] and add that to the sum of paths to the visited node, storing that as Paths[visited]. For every node in the current layer i, we also check if it is a neighbor with v. If there is at least one node in the layer i that is a neighbor with v, then we are done, and for every node in layer i that is a neighbor with v, we sum up the paths to that node. The sum of those will be the number of shortest paths. Proof of correctness The algorithm finds the shortest path because it is a form of BFS, with a modification. BFS would proven to be correct in class and in the textbook. Proof by induction. When the algorithm begins, there is trivially one way to reach u. Let i represent the layer ”away” from u. Thus the base case of i = 0. is correct. Just to be sure, we can check i = 1. There is only 1 shortest path to each of of the neighbors of u, and that is following the edge that connects u and each of u’s neighbors. So i = 1 is correct. We will assume that the algorithm holds for n - 1. We arbitrarily choose v to be in the nth ”layer” away from u. Since we assumed that the n - 1 layer away from u correctly labels each node with the number of shortest paths to that node, then we

Upload: jonathan-mares

Post on 24-Dec-2015

11 views

Category:

Documents


2 download

DESCRIPTION

Algorithms

TRANSCRIPT

Page 1: Problem Set 1

Introduction to Algorithms Problem Set 1CS 4820 Spring 2015 Due 11:59pm Thursday, January 29

name: Jonathan Mares netid: jm2242 collaborators: None

Your homework submissions need to be typeset (hand-drawn figures are OK). See the course web pagefor suggestions on typing formulas. Solution to each question needs to be uploaded to CMS as a separatepdf file. (Questions with multiple parts need to be uploaded as a single file.)

Remember that when a problem asks you to design an algorithm, you must also prove the algorithm’scorrectness and analyze its running time. The running time must be bounded by a polynomial functionof the input size.

Note: Due to delay in the textbook arriving to campus store, we posted two sections of the book onCMS that are the most helpful for the current homework.

(1) Mark Lombardi (1951-2000) http://en.wikipedia.org/wiki/Mark_Lombardi was an Amer-ican neo-conceptual artist who specialized in drawings that use famous people’s social networks todocument alleged financial and political frauds. Many of his famous drawings are graphs of social net-works of powerful individuals, connecting pairs of people with particular relationships. People thenwonder what it means when certain pairs of people are connected in this graph by a surprisingly shortpath. Of course, a single short path can be accidental, maybe many short paths are more sure tomean strong social connection. The problem here is to give an efficient algorithm to find the number ofshortest path between two points.

Given an undirected graph G with n nodes and m edges, and two nodes of interest u and v. Weare interested in shortest paths, i.e., paths with the minimum number of edges. Give an O(m+n) timealgorithm to find the length of the shortest path, and the number of different shortest paths between uand v. Please note that you do not need to output the paths themselves, only the length of the shortestpaths, and the number of different shortest paths between u and v.

Solution.

Algorithm I will assume that the nodes are labeled from 0 to n-1 if there are n nodes. To find thelength of the shortest path, a breadth first search on the graph can be conducted. To find the numberof different shortest paths between u and v, we modify the breadth first search algorithm. Beginning atnode u, in addition to visiting each neighbor node, we label the visited node with the number of pathsfrom u to that node. We can find the number of paths to that node by checking for every neighbor ofthe visited node j, if it has been visited, then we look up Paths[j] and add that to the sum of paths tothe visited node, storing that as Paths[visited]. For every node in the current layer i, we also check if itis a neighbor with v. If there is at least one node in the layer i that is a neighbor with v, then we aredone, and for every node in layer i that is a neighbor with v, we sum up the paths to that node. Thesum of those will be the number of shortest paths.

Proof of correctnessThe algorithm finds the shortest path because it is a form of BFS, with a modification. BFS wouldproven to be correct in class and in the textbook.

Proof by induction. When the algorithm begins, there is trivially one way to reach u. Let i representthe layer ”away” from u. Thus the base case of i = 0. is correct. Just to be sure, we can check i = 1.There is only 1 shortest path to each of of the neighbors of u, and that is following the edge thatconnects u and each of u’s neighbors. So i = 1 is correct. We will assume that the algorithm holds forn− 1. We arbitrarily choose v to be in the nth ”layer” away from u. Since we assumed that the n− 1layer away from u correctly labels each node with the number of shortest paths to that node, then we

Page 2: Problem Set 1

know taking the edge from v to any of v’s neighbors in the n-1 ”layer” must be one of the shortest pathsto u. Hence proven.

Running Time BFS has a running time of O(m + n). In the modified version of the algorithm,we keep track of the number of shortest paths to a particular node. This is accessing and modifying anarray, which is O(1). we have to do this for every node in the graph, which changes the running timeto O(2m + n), but the 2 is a constant which can reduce the running time to O(m + n).