Top Banner
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department
53

1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

1

Data Structures and Algorithms

Graphs I:

Representation and Search

Gal A. Kaminka

Computer Science Department

Page 2: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

2

Outline

Reminder: Graphs Directed and undirected

Matrix representation of graphs Directed and undirected

Sparse matrices and sparse graphs Adjacency list representation

Page 3: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

3

Graphs Tuple <V,E> V is set of vertices E is a binary relation on V

Each edge is a tuple < v1,v2 >, where v1,v2 in V |E| =< |V|2

Page 4: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

4

Directed and Undirected Graphs

Directed graph: < v1, v2 > in E is ordered, i.e., a relation (v1,v2)

Undirected graph: < v1, v2 > in E is un-ordered, i.e., a set { v1, v2 }

Degree of a node X: Out-degree: number of edges < X, v2 > In-degree: number of edges < v1, X > Degree: In-degree + Out-degree In undirected graph: number of edges { X, v2 }

Page 5: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

5

Examples of graphs

Page 6: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

6

Paths

Path from vertex v0 to vertex vk: A sequence of vertices < v0, v1, …. vk >

For all 0 =< i < k, edge < vi, vi+1 > exists.

Path is of length k Two vertices x, y are adjacent if < x, y > is an edge Path is simple if vertices in sequence are distinct. Cycle: if v0 = vk

< v, v > is cycle of length 1

Page 7: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

7

Connected graphs

Undirected Connected graph: For any vertices x, y there exists a path xy (= yx)

Directed connected graph: If underlying undirected graph is connected

Strongly connected directed graph: If for any two vertices x, y there exist path xy

and path yx Clique: a strongly connected component

|V|-1 =< |E| =< |V|2

Page 8: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

8

Cycles and trees

Graph with no cycles: acyclic Directed Acyclic Graph: DAG Undirected forest:

Acyclic undirected graph Tree: undirected acyclic connected graph

one connected component

Page 9: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

9

Representing graphs

Adjacency matrix: When graph is dense |E| close to |V|2

Adjacency lists: When graph is sparse |E| << |V|2

Page 10: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

10

Adjacency Matrix

Matrix of size |V| x |V| Each row (column) j correspond to a distinct vertex j

“1” in cell < i, j > if there is exists an edge <i,j> Otherwise, “0”

In an undirected graph, “1” in <i,j> => “1” in <j,i> “1” in <j,j> means there’s a self-loop in vertex j

Page 11: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

11

Examples

1 2 3

1 0 0 1

2 0 1 0

3 1 1 0

1

3

2

32

1

4

1 2 3 4

1 0 1 1 0

2 1 0 0 0

3 1 0 0 0

4 0 0 0 0

Page 12: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

12

Adjacency matrix features

Storage complexity: O(|V|2) But can use bit-vector representation

Undirected graph: symmetric along main diagonal AT transpose of A Undirected: A=AT

In-degree of X: Sum along column X O(|V|) Out-degree of X: Sum along row X O(|V|) Very simple, good for small graphs

Edge existence query: O(1)

Page 13: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

13

But, ….

Many graphs in practical problems are sparse Not many edges --- not all pairs x,y have edge xy

Matrix representation demands too much memory We want to reduce memory footprint

Use sparse matrix techniques

Page 14: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

14

Adjacency List

An array Adj[ ] of size |V| Each cell holds a list for associated vertex Adj[u] is list of all vertices adjacent to u

List does not have to be sorted

Undirected graphs: Each edge is represented twice

Page 15: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

15

Examples

1 3

2 2

3 1 2

1

3

2

32

1

4

1 2 3

2 1

3 1

4

Page 16: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

16

Adjacency list features

Storage Complexity: O(|V| + |E|) In undirected graph: O(|V|+2*|E|) = O(|V|+|E|)

Edge query check: O(|V|) in worst case

Degree of node X: Out degree: Length of Adj[X] O(|V|) calculation In degree: Check all Adj[] lists O(|V|+|E|) Can be done in O(1) with some auxiliary information!

Page 17: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

17

?שאלות

Page 18: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

18

Graph Traversals (Search)

We have covered some of these with binary trees Breadth-first search (BFS) Depth-first search (DFS)

A traversal (search): An algorithm for systematically exploring a graph Visiting (all) vertices Until finding a goal vertex or until no more vertices

Only for connected graphs

Page 19: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

19

Breadth-first search

One of the simplest algorithms Also one of the most important

It forms the basis for MANY graph algorithms

Page 20: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

20

BFS: Level-by-level traversal

Given a starting vertex s Visit all vertices at increasing distance from s

Visit all vertices at distance k from s Then visit all vertices at distance k+1 from s Then ….

Page 21: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

21

BFS in a binary tree (reminder)

BFS: visit all siblings before their descendents

5

2

1 3

8

6 10

7 95 2 8 1 3 6 10 7 9

Page 22: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

22

BFS(tree t)1. q new queue

2. enqueue(q, t)

3. while (not empty(q))

4. curr dequeue(q)

5. visit curr // e.g., print curr.datum

6. enqueue(q, curr.left)

7. enqueue(q, curr.right)

This version for binary trees only!

Page 23: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

23

BFS for general graphs

This version assumes vertices have two children left, right This is trivial to fix

But still no good for general graphs It does not handle cycles

Example.

Page 24: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

24

Start with A. Put in the queue (marked red)

A

B

G C

E

D

F

Queue: A

Page 25: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

25

B and E are next

A

B

G C

E

D

F

Queue: A B E

Page 26: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

26

When we go to B, we put G and C in the queue

When we go to E, we put D and F in the queue

A

B

G C

E

D

F

Queue: A B E C G D F

Page 27: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

27

When we go to B, we put G and C in the queue

When we go to E, we put D and F in the queue

A

B

G C

E

D

F

Queue: A B E C G D F

Page 28: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

28

Suppose we now want to expand C.

We put F in the queue again!

A

B

G C

E

D

F

Queue: A B E C G D F F

Page 29: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

29

Generalizing BFS

Cycles: We need to save auxiliary information Each node needs to be marked

Visited: No need to be put on queue Not visited: Put on queue when found

What about assuming only two children vertices? Need to put all adjacent vertices in queue

Page 30: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

30

BFS(graph g, vertex s)1. unmark all vertices in G

2. q new queue

3. mark s

4. enqueue(q, s)

5. while (not empty(q))

6. curr dequeue(q)

7. visit curr // e.g., print its data

8. for each edge <curr, V>

9. if V is unmarked

10. mark V

11. enqueue(q, V)

Page 31: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

31

The general BFS algorithm Each vertex can be in one of three states:

Unmarked and not on queue Marked and on queue Marked and off queue

The algorithm moves vertices between these states

Page 32: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

32

Handling vertices

Unmarked and not on queue: Not reached yet

Marked and on queue: Known, but adjacent vertices not visited yet (possibly)

Marked and off queue: Known, all adjacent vertices on queue or done with

Page 33: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

33

Start with A. Mark it.

A

B

G C

E

D

F

Queue: A

Page 34: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

34

Expand A’s adjacent vertices.

Mark them and put them in queue.

A

B

G C

E

D

F

Queue: A B E

Page 35: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

35

Now take B off queue, and queue its neighbors.

A

B

G C

E

D

F

Queue: A B E C G

Page 36: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

36

Do same with E.

A

B

G C

E

D

F

Queue: A B E C G D F

Page 37: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

37

Visit C.

Its neighbor F is already marked, so not queued.

A

B

G C

E

D

F

Queue: A B E C G D F

Page 38: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

38

Visit G.

A

B

G C

E

D

F

Queue: A B E C G D F

Page 39: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

39

Visit D. F, E marked so not queued.

A

B

G C

E

D

F

Queue: A B E C G D F

Page 40: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

40

Visit F.

E, D, C marked, so not queued again.

A

B

G C

E

D

F

Queue: A B E C G D F

Page 41: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

41

Done. We have explored the graph in order:

A B E C G D F.

A

B

G C

E

D

F

Queue: A B E C G D F

Page 42: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

42

Interesting features of BFS

Complexity: O(|V| + |E|) All vertices put on queue exactly once For each vertex on queue, we expand its edges In other words, we traverse all edges once

BFS finds shortest path from s to each vertex Shortest in terms of number of edges Why does this work?

Page 43: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

43

Depth-first search

Again, a simple and powerful algorithm Given a starting vertex s Pick an adjacent vertex, visit it.

Then visit one of its adjacent vertices ….. Until impossible, then backtrack, visit another

Page 44: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

44

DFS(graph g, vertex s)Assume all vertices initially unmarked

1. mark s

2. visit s // e.g., print its data

3. for each edge <s, V>

4. if V is not marked

5. DFS(G, V)

Page 45: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

45

Start with A. Mark it.

A

B

G C

E

D

F

Current vertex: A

Page 46: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

46

Expand A’s adjacent vertices. Pick one (B).

Mark it and re-visit.

A

B

G C

E

D

F

Current: B

Page 47: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

47

Now expand B, and visit its neighbor, C.

A

B

G C

E

D

F

Current: C

Page 48: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

48

Visit F.

Pick one of its neighbors, E.

A

B

G C

E

D

F

Current: F

Page 49: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

49

E’s adjacent vertices are A, D and F.

A and F are marked, so pick D.

A

B

G C

E

D

F

Current: E

Page 50: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

50

Visit D. No new vertices available. Backtrack to

E. Backtrack to F. Backtrack to C. Backtrack to B

A

B

G C

E

D

F

Current: D

Page 51: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

51

Visit G. No new vertices from here. Backtrack to

B. Backtrack to A. E already marked so no new.

A

B

G C

E

D

F

Current: G

Page 52: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

52

Done. We have explored the graph in order:

A B C F E D G

A

B

G C

E

D

F

Current:1

2

3

4

6

7

5

Page 53: 1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.

53

Interesting features of DFS

Complexity: O(|V| + |E|) All vertices visited once, then marked For each vertex on queue, we examine all edges In other words, we traverse all edges once

DFS does not necessarily find shortest path Why?