Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody....

17
Graphs and MSTs Sections 1.4 and 9.1

Transcript of Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody....

Page 1: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Graphs and MSTs

• Sections 1.4 and 9.1

Page 2: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Partial-Order Relations

•Everybody is not related to everybody. Examples?

•Direct road connections between locations

•How about using a tree to represent the connections?

Page 3: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Graphs: Definition

G = (V, E)

Vertices (nodes)

Edges (arcs)

Edges are a subset of V V

{(v,v’) : v and v’ are in V}

We also write v v’ instead of (v.v’)

Page 4: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Directed Graphs

Our PA map is undirected because if (v,v’) E then it is assumed that (v’,v) E (but not included in E)

Can you think of a situation were the graph is directed? That is, we may have (v,v’) E but not (v’,v) E

City streets. Some are one way

A directed graph or digraph is one were the order matters

v v’

Page 5: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Complete Graphs

A complete graph is one where there is an edge between every two nodes

If the graph has N nodes and the graph is complete and undirected, how many edges are there?

(N-1)N/2

If the digraph has N nodes and the digraph is complete, how many edges are there?

(N-1)N

Page 6: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Subgraphs

Given a graph G = (V, E) and a graph G’ = (V’, E’), G is a subgraph of G’ if:

•V V’•E E’

Every element in the left set is an element in the right set

Page 7: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Paths

Given a graph G = (V, E), a path between two elements v, v’ in V is a sequence of elements: v

Quiz: Obtain all the paths in the following digraph:

2 3

7

v1 … vNsuch that v v1, v1v2,…,

v2v’

vNv’ are all edges in E

The length of the path is the number of edges on it

Page 8: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Weighted Graphs

Given a weighted graph G = (V, E), a path between two elements v, v’ in V : v v1 … v’, the weight or cost of the path is the summation of the weight of the edges in the path

A weighted graph G = (V, E), is a graph such that each edge v v’ has an associated number (called the weight)

Page 9: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Connected Graphs

A graph G = (V, E) is connected if there is at least one path between any two elements

2 3

75

Connected?

Yes

Page 10: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Acyclic Graphs

Given a graph G = (V, E), a cycle is a path of at least length 1 starting with a node and ending in the same node

A graph G = (V, E) is acyclic if it has no cycles

2 3

75

Acyclic?

No

Page 11: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Trees

A tree is a connected acyclic graph G = (V, E)

Number of arcs in a tree with |V| nodes:

|E|-1

Page 12: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Techniques for Design of Algorithms

•Brute Force: follows directly from the definition

Exhaustive search: particular form of Brute Force

•Greedy Algorithm: Obtain solution by expanding partial solution constructed so far. On each step a decision is made that is:

FeasibleLocally optimalIrrevocable

Page 13: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Consider the following undirected graph:

B

CA

H

E

D

G

F

Example: Greedy algorithm for TSP

2

37

2

42

2

2 2

2

2

6

8

7

Use a Greedy technique to obtain a solution of the TSP

Does using a Greedy technique always solves TSPs?Complexity (n = # arcs)? O(n)

No

Page 14: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

The Minimum Spanning Tree

Give a graph G = (V, E), the minimum spanning tree (MST) is a weighted graph G’ = (V, E’) such that:

•E’ E•G’ is connected•G’ has the minimum cost

Page 15: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

VV’

If there is an edge e between the 2 groups with minimum cost, then there is an MST containing e

Why does it works?Property: Suppose that we divide the nodes of a graph G = (V, E) in two groups V, V’:

5

7

10

22

66

12

613

2020

Page 16: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

VisitedNon visited

Example of a non greedy algorithm?

Example of a “Greedy” Algorithm

Dijkstra-Prim is an example of a greedy algorithm since it looks at a subset of the larger problem

5

7

10

22

66

12

613

2020

Our naïve algorithm for MST

Unfortunately, not all problems can be solved with greedy algorithms

Page 17: Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.

Complexity

• Actual complexity is O((|E|+|V|)log2 |E|)

• It is easy to see that it is at most |E||V|

e1e2

e1 + (e1 + e2) +…+ (e1 + e2 + … +e2) |E||V|