cse 326: data structures: graphs

36
1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003

Upload: russ

Post on 27-Jan-2016

44 views

Category:

Documents


0 download

DESCRIPTION

CSE 326: Data Structures: Graphs. Lecture 19: Monday, Feb 24, 2003. Outline. Graphs: Definitions Properties Examples Topological Sort Graph Traversals Depth First Search Breadth First Search. TO DO: READ WEISS CH 9. Han. Luke. Leia. Graphs. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSE 326: Data Structures:  Graphs

1

CSE 326: Data Structures: Graphs

Lecture 19: Monday, Feb 24, 2003

Page 2: CSE 326: Data Structures:  Graphs

2

Outline

• Graphs:– Definitions– Properties– Examples

• Topological Sort

• Graph Traversals– Depth First Search– Breadth First Search

TO DO: READ WEISS CH 9

Page 3: CSE 326: Data Structures:  Graphs

3

GraphsGraphs = formalism for representing relationships

between objects• A graph G = (V, E)

– V is a set of vertices– E V V is a set of edges

Han

Leia

Luke

V = {Han, Leia, Luke}E = {(Luke, Leia), (Han, Leia), (Leia, Han)}

Page 4: CSE 326: Data Structures:  Graphs

4

Directed vs. Undirected Graphs

Han

Leia

Luke

Han

Leia

Luke

• In directed graphs, edges have a specific direction:

• In undirected graphs, they don’t (edges are two-way):

• Vertices u and v are adjacent if (u, v) E

Page 5: CSE 326: Data Structures:  Graphs

5

Weighted Graphs

20

30

35

60

Mukilteo

Edmonds

Seattle

Bremerton

Bainbridge

Kingston

Clinton

There may be more information in the graph as well.

Each edge has an associated weight or cost.

Page 6: CSE 326: Data Structures:  Graphs

6

Paths and CyclesA path is a list of vertices {v1, v2, …, vn} such

that (vi, vi+1) E for all 0 i < n.A cycle is a path that begins and ends at the same

node.

Seattle

San FranciscoDallas

Chicago

Salt Lake City

p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle}

Page 7: CSE 326: Data Structures:  Graphs

7

Path Length and Cost

Path length: the number of edges in the pathPath cost: the sum of the costs of each edge

Seattle

San FranciscoDallas

Chicago

Salt Lake City

3.5

2 2

2.5

3

22.5

2.5

length(p) = 5 cost(p) = 11.5

Page 8: CSE 326: Data Structures:  Graphs

8

ConnectivityUndirected graphs are connected if there is a

path between any two vertices

Directed graphs are strongly connected if there is a path from any one vertex to any other

Page 9: CSE 326: Data Structures:  Graphs

9

ConnectivityDirected graphs are weakly connected if there

is a path between any two vertices, ignoring direction

A complete graph has an edge between every pair of vertices

Page 10: CSE 326: Data Structures:  Graphs

10

Connectivity

A (strongly) connected component is a subgraph which is (strongly) connected

CC in an undirected graph:

SCC in a directed graph:

Page 11: CSE 326: Data Structures:  Graphs

11

Distance and Diameter

• The distance between two nodes, d(u,v), is the length of the shortest paths, or if there is no path

• The diameter of a graph is the largest distance between any two nodes

• Graph is strongly connected iff diameter <

Page 12: CSE 326: Data Structures:  Graphs

12

An Interesting Graph

• The Web !

• Each page is a vertice• Each hyper-link is an edge

• How many nodes ?• Is it connected ?• What is its diameter ?

Page 13: CSE 326: Data Structures:  Graphs

13

The Web Graph

Page 14: CSE 326: Data Structures:  Graphs

14

Other Graphs

• Geography:– Cities and roads– Airports and flights (diameter 20 !!)

• Publications:– The co-authorship graph

• E.g. the Erdos distance

– The reference graph

• Phone calls: who calls whom• Almost everything can be modeled as a graph !

Page 15: CSE 326: Data Structures:  Graphs

15

Graph Representation 1: Adjacency Matrix

A |V| x |V| array in which an element (u, v) is true if and only if there is an edge from u to vHan

Leia

Luke

Han Luke Leia

Han

Luke

LeiaRuntime:iterate over verticesiterate ever edgesiterate edges adj. to vertexedge exists?

Space requirements:

Page 16: CSE 326: Data Structures:  Graphs

16

Graph Representation 2: Adjacency List

A |V|-ary list (array) in which each entry stores a list (linked list) of all adjacent verticesHan

Leia

LukeHan

Luke

Leia

space requirements:

Runtime:iterate over verticesiterate ever edgesiterate edges adj. to vertexedge exists?

Page 17: CSE 326: Data Structures:  Graphs

17

Graph Density

A sparse graph has O(|V|) edges

A dense graph has (|V|2) edges

Anything in between is either sparsish or densy depending on the context.

Page 18: CSE 326: Data Structures:  Graphs

18

Trees as Graphs

• Every tree is a graph with some restrictions:– the tree is directed– there are no cycles

(directed or undirected)– there is a directed path

from the root to every node

A

B

D E

C

F

HG

JI

BAD!

Page 19: CSE 326: Data Structures:  Graphs

19

Directed Acyclic Graphs (DAGs)

DAGs are directed graphs with no cycles.

main()

add()

access()

mult()

read()

Trees DAGs Graphs

if program call graph is a DAG, then all procedure calls can be in-lined

Page 20: CSE 326: Data Structures:  Graphs

20

Application of DAGs: Representing Partial Orders

check inairport

calltaxi

taxi toairport

reserveflight

packbagstake

flight

locategate

Page 21: CSE 326: Data Structures:  Graphs

21

Topological Sort

Given a graph, G = (V, E), output all the vertices in V such that no vertex is output before any other vertex with an edge to it.

check inairport

calltaxi

taxi toairport

reserveflight

packbags

takeflight

locategate

Page 22: CSE 326: Data Structures:  Graphs

22

Topological Sort

check inairport

calltaxi

taxi toairport

reserveflight

packbags

takeflight

locategate

Page 23: CSE 326: Data Structures:  Graphs

23

Topo-Sort Take One

Label each vertex’s in-degree (# of inbound edges)While there are vertices remaining

Pick a vertex with in-degree of zero and output itReduce the in-degree of all vertices adjacent to itRemove it from the list of vertices

Label each vertex’s in-degree (# of inbound edges)While there are vertices remaining

Pick a vertex with in-degree of zero and output itReduce the in-degree of all vertices adjacent to itRemove it from the list of vertices

runtime:

Page 24: CSE 326: Data Structures:  Graphs

24

Topo-Sort Take Two

Label each vertex’s in-degreeInitialize a queue to contain all in-degree zero vertices

While there are vertices remaining in the queueRemove a vertex v with in-degree of zero and output itReduce the in-degree of all vertices adjacent to vPut any of these with new in-degree zero on the queue

Label each vertex’s in-degreeInitialize a queue to contain all in-degree zero vertices

While there are vertices remaining in the queueRemove a vertex v with in-degree of zero and output itReduce the in-degree of all vertices adjacent to vPut any of these with new in-degree zero on the queue

runtime:Can we use a stack instead of a queue ?

Page 25: CSE 326: Data Structures:  Graphs

25

Topo-Sort

• So we sort again in linear time: O(|V|+|E|)

• Can we apply this to normal sorting ?If we sort an array A[1], A[2], ..., A[n] using topo-sort, what is the running time ?

Page 26: CSE 326: Data Structures:  Graphs

26

Recall: Tree Traversals

a

i

d

h j

b

f

k l

ec

g

a b f g k c d h i l j e

Page 27: CSE 326: Data Structures:  Graphs

27

Depth-First Search• Pre/Post/In – order traversals are examples of

depth-first search– Nodes are visited deeply on the left-most branches

before any nodes are visited on the right-most branches• Visiting the right branches deeply before the left would still be

depth-first! Crucial idea is “go deep first!”

• Difference in pre/post/in-order is how some computation (e.g. printing) is done at current node relative to the recursive calls

• In DFS the nodes “being worked on” are kept on a stack

Page 28: CSE 326: Data Structures:  Graphs

28

DFS

void DFS(Node startNode) {Stack s = new Stack;

s.push(startNode);while (!s.empty()) {

x = s.pop();/* process x */for y in x.children() do

s.push(y);}

void DFS(Node startNode) {Stack s = new Stack;

s.push(startNode);while (!s.empty()) {

x = s.pop();/* process x */for y in x.children() do

s.push(y);}

Page 29: CSE 326: Data Structures:  Graphs

29

DFS on Trees

a

i

d

h j

b

f

k l

ec

g

a

b

g

k

Page 30: CSE 326: Data Structures:  Graphs

30

DFS on Graphs

a

i

d

h j

b

f

k l

ec

g

a

b

g

k

Problem with cycles: may run into an infinite loop

Page 31: CSE 326: Data Structures:  Graphs

31

DFS

Running time:

void DFS(Node startNode) {for v in Nodes do

v.visited = false;Stack s = new Stack;

s.push(startNode);while (!s.empty()) {

x = s.pop();x.visited = true;/* process x */for y in x.children() do

if (!y.visited) s.push(y);}

void DFS(Node startNode) {for v in Nodes do

v.visited = false;Stack s = new Stack;

s.push(startNode);while (!s.empty()) {

x = s.pop();x.visited = true;/* process x */for y in x.children() do

if (!y.visited) s.push(y);}

Page 32: CSE 326: Data Structures:  Graphs

32

Breadth-First Search• Traversing tree level by level from top to bottom

(alphabetic order)a

i

d

h j

b

f

k l

ec

g

Page 33: CSE 326: Data Structures:  Graphs

33

Breadth-First Search

BFS characteristics

• Nodes being worked on maintained in a FIFO Queue, not a stack

• Iterative style procedures often easier to design than recursive procedures

Page 34: CSE 326: Data Structures:  Graphs

34

Breadth-First Searchvoid DFS(Node startNode) {

for v in Nodes dov.visited = false;

Stack s = new Stack;

s.push(startNode);while (!s.empty()) {

x = s.pop();x.visited = true;/* process x */for y in x.children() do

if (!y.visited) s.push(y);}

void DFS(Node startNode) {for v in Nodes do

v.visited = false;Stack s = new Stack;

s.push(startNode);while (!s.empty()) {

x = s.pop();x.visited = true;/* process x */for y in x.children() do

if (!y.visited) s.push(y);}

void BFS(Node startNode) {for v in Nodes do

v.visited = false;Queue s = new Queue;

s.enqueue(startNode);while (!s.empty()) {

x = s.dequeue();x.visited = true;/* process x */for y in x.children() do

if (!y.visited) s.enqueue(y);}

void BFS(Node startNode) {for v in Nodes do

v.visited = false;Queue s = new Queue;

s.enqueue(startNode);while (!s.empty()) {

x = s.dequeue();x.visited = true;/* process x */for y in x.children() do

if (!y.visited) s.enqueue(y);}

Page 35: CSE 326: Data Structures:  Graphs

35

QUEUE

ab c d ec d e f gd e f ge f g h i jf g h i jg h i jh i j ki j kj k lk ll

a

i

d

h j

b

f

k l

ec

g

Page 36: CSE 326: Data Structures:  Graphs

36

Graph TraversalsDFS and BFS

• Either can be used to determine connectivity:– Is there a path between two given vertices?– Is the graph (weakly) connected?

• Important difference: Breadth-first search always finds a shortest path from the start vertex to any other (for unweighted graphs)– Depth first search may not!