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

39
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

Post on 19-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

1

Discrete Structures & Algorithms

Graphs and Trees: II

EECE 320

Page 2: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

2

Topics for the day

Some examplesQuestions on graphs and solutions

Ideas for other proofs

Minimal spanning trees

Page 3: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

3

Spanning TreesA spanning tree of a graph G is a tree that touches every node of G and uses only edges from G

Every connected graph has a spanning tree

Page 4: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

4

Example 1

Prove: A graph G is 2-colourable iff it contains no cycle of odd length.

What does 2-colorable mean?• We can color the vertices of the graph with at

most two colors such that two adjacent nodes do not get the same color.

Adjacent nodes?• Two vertices are adjacent if there is an edge

connecting the two vertices.

Cycle? Of odd length?• If there is a path that starts at a vertex and

ends at the same vertex without repeating an edge, then the graph has a cycle.

• The number of edges along the cycle is the length of the cycle.

Page 5: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

5

Prove: A graph G is 2-colourable iff it contains no cycle of odd length.

First: Assume that the graph is 2-colourable and prove that it contains no odd cycle.

• Select a 2-colouring of G. Consider an arbitrary cycle v1, v2, …, vk, v1.

• The vertices with even subscript must be one colour and the vertices with odd subscripts must be a different colour.

• Since v1 and vk must be coloured differently, k must be even.

• But, k is the length of the cycle so all cycles must be of even length.

v1

v2

v3

v4

Page 6: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

6

Prove: A graph G is 2-colourable iff it contains no cycle of odd length.

Second: Assume that the graph has no cycle of odd length and prove that it must be 2-colourable.• If we can 2-color every connected component of G, then we can 2-color all of G. Thus, it suffices to show that we can 2-color an arbitrary component of G.

• Consider an arbitrary component H of G.• Consider some spanning tree T of H.• Choose any 2-colouring of T. Any tree can be

coloured with at most 2 colours because we can think of a tree as having parents and children and we colour each generation separately.

• Let x-y be some edge not in T and consider the path from some vertex v to x. Let z be the last vertex on the path from v to x that also lies on the path from v to y.

• Of the paths from z to x and from z to y, exactly one must have odd length else the these two paths together with the edge x-y would form an odd length cycle.

• Thus x and y belong to different generations wrt z and will have different colours.

v

x

y

H

w

z

Page 7: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

7

Example 2

Problem: If a graph G has 15 edges and its complementary graph G’ has 13 edges, how many vertices does G have?

v1

v2

v3

v4

G

v2

v3

v4 G’

v1

Complementary graph G’: An edge between vertices x and y exists in G’ iff x and y are not

adjacent in G.

Page 8: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

8

Problem: If a graph G has 15 edges and its complementary graph G’ has 13 edges, how many vertices does G have?

•The maximum number of edges a graph with n vertices can have is n(n-1)/2.

•The complement of a complete graph has no edges.

•If E is the set of edges in G and E’ the set of edges in G’ then: |E|+|E’| = n(n-1)/2.

•Solve for n when n(n-1)/2 = 28.•n = 8.

Page 9: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

9

Use elementary properties of graphs to construct proofs. Start with properties that you understand easily:

degree, number of edges and number of vertices, paths, cycles, complements, spanning trees, dual graph, …

Page 10: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

10

Finding Optimal Trees

Trees have many nice properties (uniqueness of paths, no cycles, etc.)

We may want to compute the “best” tree approximation to a graph

If all we care about is communication, then a tree may be enough. We want a tree with smallest communication link costs

Page 11: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

11

Finding Optimal Trees

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

Page 12: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

12

4

8

79

6

11

9

5

87

Tree Approximations

Page 13: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

13

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.

Page 14: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

14

1

8

7

9

10

3

5

47

9

Applying the Algorithm

Page 15: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

15

Proving Correctness of the AlgorithmThe algorithm outputs a spanning tree T.

Then let M be a minimum spanning tree. M≠T Let e be the first edge chosen by the algorithm that is in T but not in M.

N = M+e-f is another spanning tree.

[By contradiction] Suppose that it’s not minimal. (For simplicity, assume all edge weights in graph are distinct)

If we add e to M, it creates a cycle. Since this cycle isn’t fully contained in T, it has (at least) an edge f not in T.

Page 16: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

16

Proving Correctness of the Algorithm (cont)

N = M+e-f is another spanning tree.

Claim: e < f, and therefore N < M

Suppose not: e > f

Then f would have been visited before e by the algorithm, but not added, because adding it would have formed a cycle.

Then N < M this M is not a minimal spanning tree. Contradiction!

Page 17: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

17

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?

Page 18: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

18

TSP: Traveling Salesman Problem

Given: a number of cities and the costs of traveling from any city to any other city,

Q: What is the cheapest round-trip route that visits each city exactly once and then returns to the starting city?

The problem is in NP

Page 19: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

19

TSP from Trees

We can use an MST to derive a TSP tour that is no more expensive than twice the optimal tour.Idea: walk “around” the MST and take shortcuts if a node has already been visited.

Modified TSP: We assume that all pairs of nodes are connected, and edge weights satisfy the triangle inequality

d(x,y) d(x,z) + d(z,y)

Complexity still NP

Page 20: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

20

Tours from Trees

This is a 2-competitive algorithm

Shortcuts only decrease the cost, so Cost(Greedy Tour) 2 Cost(MST) 2 Cost(Optimal Tour)

Page 21: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

21

Shortest-paths in a graph

Page 22: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

22

Shortest path problem Input data:

Directed graph: G= (V, E) Start and end node: s, t Length le for each edge e

Output: shortest path from s to t [cost of path = sum of all edges]

Page 23: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

23

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

Page 24: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

24

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

Page 25: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

25

Example

Page 26: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

26

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

Page 27: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

27

Bipartite Graph

A graph is bipartite if the nodes can be partitioned into two sets V1 and V2 such that all edges go only between V1 and V2 (no edges go from V1 to V1 or from V2 to V2)

Page 28: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

28

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?

Page 29: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

29

Dancing Partners

Page 30: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

30

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”

Page 31: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

31

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

Page 32: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

32

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.

Page 33: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

33

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

Page 34: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

34

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

Page 35: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

35

Proof 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

Page 36: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

36

Proof of Marriage Theorem

Select two nodes a A and b B connected by an edge

Idea: Take G1 = (a,b) and G2 = everything else

Problem: G2 need not be matchable. There could be a set of k nodes that has only k-1 neighbors.

Page 37: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

37

k-1k

a bThe 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 G1, and take G2 to be everything else.

Page 38: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

38

More formally …

Page 39: 1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320

41

Here’s What You Need to Know…

Minimum Spanning Tree

- Definition

Kruskal’s Algorithm

- Definition

- Proof of Correctness

Traveling Salesman Problem

- Definition

- Using MST to get an

approximate solution

The Marriage Theorem