15-211 fundamental data structures and algorithms (spring ’05) recitation notes: graphs slides...

28
15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@cs Based on recitation notes by David Murray

Upload: shanon-cook

Post on 05-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Fundamental Data Structures and Algorithms (Spring ’05)Recitation Notes: Graphs

Slides prepared by Uri Dekel, udekel@cs

Based on recitation notes by David Murray

Page 2: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

Definitions

Page 3: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

3

What is a graph?

G=(V,E) V: a set of vertices (or “nodes”) E: a set of edges (or “links”)

Every edge has two endpoints In a directed graph, endpoint order matters

i.e., (v1,v2) != (v2, v1) In a weighted graph, every edge (and possibly node)

has a weight.

Page 4: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

4

Graph Examples Undirected graphs

Computer networks

Highway systems

London

Birmingham

BristolDublin

Sheffield

Belfast

Liverpool

Manchester

Dover

Calais

Paris

Brussels

Derry

Limerick

Page 5: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

5

Graph Examples

Directed Graphs Street maps with

one-way and two-way streets

Game boards (e.g,”ladders and chutes”)

Page 6: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

6

Graph Examples

Weighted graphs Computer networks

e.g., Minimal time for packet routing Highways with distances

e.g., “What is the shortest driving distance from Pittsburgh to New York City?”

Philadelphia

Allentown

Harrisburg

Pittsburgh

New YorkYoungstown

State College

163

58

187

62

203

79

7795

94

Page 7: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

7

Graph Terms

Loop An edge whose two endpoints are the same

Parallel edges Two different edges with the same endpoints A “simple graph” has no parallel edges

Page 8: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

8

Graph Terms Path

A consecutive path of vertices and edges which starts at an “origin” vertex and ends at a “terminus” vertex

Circuit A path where the origin and terminus are the same

Simple path A path where no vertices are repeated (except origin in a

simple circuit)

Philadelphia

Allentown

Harrisburg

Pittsburgh

New YorkYoungstown

State College

163

58

187

62

203

79

7795

94

Non-simple pathSimple Circuit

Page 9: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

9

Graph Terms Connected graph

A graph where there is a path from every vertex to any other vertex

Luxembourg

London

Birmingham

BristolDublin

Sheffield

Belfast

Liverpool

Manchester

Dover

Calais

Paris

Brussels

Derry

Limerick

Connected

Connected

Not connected !

Page 10: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

10

Trees Tree

A connected graph with no cycles i.e., any connected graph with no cycles is a tree

Tree Not a tree

Page 11: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

Programmatic Representation

Page 12: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

12

Representing graphs Incidence matrix

Every row corresponds to a vertex Every column corresponds to an edge Entry is 1 if edge exists, 2 if loop Use negatives to indicate direction

E3

AE1

B C

D E

E4

E2

E5

E6

1 0

11

0 1

00

0 0

01

0 0

10

1

2 0

10

1 1

00

0 00 0

E2E1 E4E3 E6E5

A

B

C

D

E 1

Page 13: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

13

Representing simple graphs Adjacency matrix

Every row and column corresponds to a vertex Entry corresponds to edges Can use weights for given edges Symmetric around diagonal if graph is undirected

E3

AE1

B C

D E

E4

E2

E5

E6

0 E1

0E1

0 E4

0E2

0

E5

0

E6

E3 0

00

0 E2

0E4

0 E60 E5

BA DC E

A

B

C

D

E 0

Page 14: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

14

Representing simple graphs

Adjacency list A set of linked lists or arrays, one for each vertex,

listing the connected vertices or edges Commonly used for sparse graphs

AB

AB

D

EC

C

E

C B

AD

DE

B

E3

AE1

B C

D E

E4

E2

E5

E6

Page 15: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

Mazes and Spanning Trees

Page 16: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

16

Mazes

A maze can be considered as a graph Every “square” is a vertex If there is no wall between two adjacent squares,

an edge will link the corresponding vertices There must be a single path from entrance vertex

to exit vertex, and no cycle To construct a maze: create a spanning tree

from the entrance vertex

Page 17: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

17

Spanning tree

The spanning of a graph G is a tree containing all vertices of G More than one possible spanning tree

A

A B C

E F

D

G H

A

A B C

E F

D

G H

A

A B C

E F

D

G H

A

A B C

E F

D

G H

Page 18: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

18

Minimum Spanning Tree

A Minimum Spanning Tree of a graph G with weights on the edges A spanning tree covering all the vertices of G with

the lowest possible total weight Note: there could be more than one MST, but all

have the same weight

Page 19: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

19

Kruskal’s Algorithm for MSTs Input:

Connected graph G=(V,E) and a set of weights Output:

Minimum spanning tree T=(V,E’) Algorithm:

Start with empty T 1. Find edge e with smallest weight 2. If adding e to T does not create cycle, add it. 3. Continue until all vertices of G are in T.NOTE: Intermediate T is not necessarily a tree!

(No cycles, but does not cover all of G)

Page 20: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

20

Kruskal’s Example

Input graph:

Step #1: Choose an edge (we’ll start with A-E although G-H is also 1)

A

A B C

E

4D

G H4

2 4

3 2 1

1 2 3F

A

A

E1

Page 21: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

21

Kruskal’s Example

Input graph:

Step #2: Choose G-H

A

A B C

E

4D

G H4

2 4

3 2 1

1 2 3F

A

A

E G H

1

1

Page 22: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

22

Kruskal’s Example

Input graph:

Step #3: Choose B-F

A

A B C

E

4D

G H4

2 4

3 2 1

1 2 3F

A

A

E G H

1

1

B

2

F

Page 23: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

23

Kruskal’s Example

Input graph:

Step #4: Choose B-C

A

A B C

E

4D

G H4

2 4

3 2 1

1 2 3F

A

A

E G H

1

1

B

2

F

B C2

Page 24: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

24

Kruskal’s Example

Input graph:

Step #5: Choose F-G

A

A B C

E

4D

G H4

2 4

3 2 1

1 2 3F

A

A

E G H

1

1

B

2

F

B C2

2

Page 25: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

25

Kruskal’s Example

Input graph:

Step #6: Choose E-F

A

A B C

E

4D

G H4

2 4

3 2 1

1 2 3F

A

A

E G H

1

1

B

2

F

B C2

23

Page 26: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

26

Kruskal’s Example - Results

Step #7 (Choose C-D):

or…

Step #7 (Choose H-D):A

A

E G H

1

1

B

2

F

B C2

23

D

H4

A

A

E G H

1

1

B

2

F

B C2

23

D

H

4

Page 27: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

15-211 Recitation notes on graphs, Uri Dekel and David Murray

27

Prim’s Algorithm for MSTs Input:

Connected graph G=(V,E) and a set of weights Vertex V0 to start from

Output: Minimum spanning tree T=(V,E’) rooted at V0

Algorithm: Start with T containing only V0 While there are still vertices not in T:

Find minimal edge e between tree vertex and non-tree vertex Add e to T

Page 28: 15-211 Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, udekel@csudekel@cs Based on recitation

Questions?