last lesson graphs today graphs (implementation, traversal)

21
Last lesson Graphs Today Graphs (Implementation, Traversal)

Post on 21-Dec-2015

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

Last lesson Graphs

Today Graphs (Implementation, Traversal)

Page 2: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

Implementation

There are different approaches Adjacency list

Each node has a list of outgoing edges Perhaps also list of incoming edges

nodes stored as array, or list, or symbol table (with label as key)

Adjacency matrix Two-dimensional array A

Entry A[i][j] is 1 (or cost) if there is an edge from vi to vj

Often also keep symbol table to map label to index of node

Incidence matrix Two-dimensional array I

Entry I[i][j] is 1 (or cost) if node vi is connected with edge ej

Page 3: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

Adjacency List

Nodes Adjacencies

a b, e

b a, c, e

c b, d

d c, e

e a, b, d

b

c

d

ea

Page 4: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

Adjacency List

Adjacency list representation of the graph shown above.The elements in list i represent nodes adjacent to i and the cost of the connecting edge.

Page 5: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

Adjacency and Incidence Matrix

Adjacency Matrix

Incidence Matrix

a b c d e

a 0 1 0 0 1

b 1 0 1 0 1

c 0 1 0 1 0

d 0 0 1 0 1

e 1 1 0 1 0

1 2 3 4 5 6

a 1 1 0 0 0 0

b 0 1 1 0 1 0

c 0 0 0 0 1 1

d 0 0 0 1 0 1

e 1 0 1 1 0 0

b

c

d

ea12

34

56

Page 6: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

Graph Data Structure: Basic Operations

Insert node Insert edge from one node to another Delete edge Delete node

Must also delete adjacent edges

Given edge, find start and finish node Go though all nodes Given node, go through edges out of it Given node, go through edges into it

Page 7: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

Complex Operations

Find whether there is a path from one node to another If edges have costs, find least cost path Find all nodes reachable from one node

Find whether there is a cycle Find a sequence of nodes that is consistent with all

directed edges No edge goes backwards in the sequence

Page 8: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

Graph Traversal

Do something with each node “visit” the node

Different algorithms visit in different orders Can be used as basis for many code cliches

E.g. count the nodes E.g. find maximum/minimum/sum of a quantity

Also the basis for important graph calculations

Page 9: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

Overview

Keep a collection of nodes that are waiting to be visited

Start at one node s When you visit node v

Consider all nodes that can be reached in one step from v They are the finish for each edge starting at v

Any such node can be added to the waiting collection Unless its already there

Vital choice: how to choose next node from the waiting collection? Breadth-first Search (BFS) Depth-first Search (DFS) Topological Sort

Page 10: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

Breadth-first Search

Waiting nodes kept in queue Initially, insert one node Dequeue a node

Visit it Enqueue any adjacent node that is not already visited or

waiting

Repeat till queue is empty This will visit all nodes which can be reached from initial

one

Repeat with a new (as yet unvisited) initial node Do BFS from each Until all nodes have been visited

Page 11: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

BFS Example

r s t u

v w x y

Page 12: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

BFS Example

0

r s t u

v w x y

sQ:

Page 13: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

BFS Example

1

0

1

r s t u

v w x y

wQ: r

Page 14: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

BFS Example

1

0

1

2

2

r s t u

v w x y

rQ: t x

Page 15: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

BFS Example

1

2

0

1

2

2

r s t u

v w x y

Q: t x v

Page 16: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

BFS Example

1

2

0

1

2

2

3

r s t u

v w x y

Q: x v u

Page 17: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

BFS Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: v u y

Page 18: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

BFS Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: u y

Page 19: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

BFS Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: y

Page 20: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

BFS Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: Ø

Page 21: Last lesson  Graphs  Today  Graphs (Implementation, Traversal)

Implementation

Local variables for the traversal routine Queue of waiting nodes Currently visited node

Need to find all nodes adjacent to it Call routine of Graph class Efficient with Adjacency List representation

Way to test if a node has been visited or is waiting Symbol table as Set Or, keep extra boolean in each node object

Run-time cost O(E+V) with adjacency list representation