Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Discrete Structures & Algorithms Graphs and Trees: III EECE 320."— Presentation transcript:

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

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

3 3 Finding a MST: Kruskal’s 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.

4 4 1 8 7 9 10 3 5 4 7 9 Applying the Algorithm

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?

6 6 Greedy Algorithms vs. Backtracking 3 1 8 7 9 10 5 4 7 9

7 7 Shortest-paths in a graph

8 8

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]

10 10 Dijkstra’s algorithm Idea: Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u.

11 11 Dijkstra’s 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

12 12 Example

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).

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 )

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?

16 16 Dancing Partners

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”

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

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.

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

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

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

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.

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.

25 25 More formally …

26 26 Generalized Marriage: Hall’s 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

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

28 28 Here’s What You Need to Know… Minimum Spanning Tree - Definition Kruskal’s Algorithm - Definition - Proof of Correctness Dijkstra’s algorithm - algorithm - proof of correctness The Marriage Theorem


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

Similar presentations


Ads by Google