1 discrete structures & algorithms graphs and trees: iii eece 320

Download 1 Discrete Structures & Algorithms Graphs and Trees: III EECE 320

Post on 14-Dec-2015

219 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Slide 1

1 Discrete Structures & Algorithms Graphs and Trees: III EECE 320 Slide 2 2 Reminder: Spanning Trees A spanning tree of a graph G is a tree that touches every node of G and uses only edges from G Problem: Find a minimum spanning tree, that is, a tree that has a node for every node in the graph, such that the sum of the edge weights is minimum 4 8 7 9 6 11 9 5 8 7 Slide 3 3 Finding a MST: Kruskals Algorithm Create a forest where each node is a separate tree Make a sorted list of edges S While S is non-empty: Remove an edge with minimal weight If it connects two different trees, add the edge. Otherwise discard it. Slide 4 4 1 8 7 9 10 3 5 4 7 9 Applying the Algorithm Slide 5 5 Greed is Good (In this case) The greedy algorithm, by adding the least costly edges in each stage, succeeds in finding an MST Obviously greedy approaches do not work for all problems Some examples where they do work? Slide 6 6 Greedy Algorithms vs. Backtracking 3 1 8 7 9 10 5 4 7 9 Slide 7 7 Shortest-paths in a graph Slide 8 8 Slide 9 9 Shortest path problem Input data: Directed graph: G= (V, E) Start and end node: s, t Length l e for each edge e Output: shortest path from s to t [cost of path = sum of all edges] Slide 10 10 Dijkstras algorithm Idea: Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u. Slide 11 11 Dijkstras algorithm Idea: Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u. Initialize S = {s}, d(s) = 0. Repeatedly choose unexplored node v to minimize add v to S, and set d(v) = (v). Until all nodes are chosen Shortest path from a node in u to any other unexplored node Slide 12 12 Example Slide 13 13 Dijkstra: Complexity analysis Initialize S = {s}, d(s) = 0. Repeatedly choose unexplored node v to minimize add v to S, and set d(v) = (v). Slide 14 14 Bipartite Graphs A graph is bipartite if the nodes can be partitioned into two sets V 1 and V 2 such that all edges go only between V 1 and V 2 (no edges go from V 1 to V 1 or from V 2 to V 2 ) Slide 15 15 Dancing Partners A group of 100 boys and girls attend a dance. Every boy knows 5 girls, and every girl knows 5 boys. Can they be matched into dance partners so that each pair knows each other? Slide 16 16 Dancing Partners Slide 17 17 Perfect Matchings Perfect Matching: A matching that covers all nodes in the graph with one edge going to each node. Theorem: If every node in a bipartite graph has the same degree d 1, then the graph has a perfect matching. Note: if degrees are the same then |A| = |B|, where A is the set of nodes on the left and B is the set of nodes on the right Slide 18 18 If there are m boys, there are md edges If there are n girls, there are nd edges Proof: Claim: If all nodes have the same degree then |A| = |B| A Matter of Degree Slide 19 19 The Marriage Theorem Theorem: A bipartite graph has a perfect matching if and only if |A| = |B| and for all k [1,n]: for any subset of k nodes of A there are at least k nodes of B that are connected to at least one of them. Slide 20 20 The condition fails for this graph The Marriage Theorem For any subset of (say) k nodes of A there are at least k nodes of B that are connected to at least one of them Slide 21 21 k At most n-k n-k At least k The condition of the theorem still holds if we swap the roles of A and B: If we pick any k nodes in B, they are connected to at least k nodes in A The Feeling is Mutual Slide 22 22 Proof Sketch of Marriage Theorem Call a bipartite graph matchable if it has the same number of nodes on left and right, and any k nodes on the left are connected to at least k on the right Strategy: Break up the graph into two matchable parts, and recursively partition each of these into two matchable parts, etc., until each part has only two nodes Slide 23 23 Proof Sketch of Marriage Theorem Select two nodes a A and b B connected by an edge Idea: Take G 1 = (a,b) and G 2 = everything else Problem: G 2 need not be matchable. There could be a set of k nodes that has only k-1 neighbors. Slide 24 24 k-1 k a b The only way this could fail is if one of the missing nodes is b This is a matchable partition! Sketch of Marriage Theorem Proof Add this in to form G 1, and take G 2 to be everything else. Slide 25 25 More formally Slide 26 26 Generalized Marriage: Halls Theorem Let S = {S 1, S 2, } be a set of finite subsets that satisfies: For any subset T = {T i } of S, | T i | |T|. Thus, any k subsets contain at least k elements Then we can choose an element x i S i from each S i so that {x 1, x 2, } are all distinct Slide 27 27 Suppose that a standard deck of cards is dealt into 13 piles of 4 cards each Then it is possible to select a card from each pile so that the 13 chosen cards contain exactly one card of each rank Example Slide 28 28 Heres What You Need to Know Minimum Spanning Tree - Definition Kruskals Algorithm - Definition - Proof of Correctness Dijkstras algorithm - algorithm - proof of correctness The Marriage Theorem

Recommended

View more >