Last lesson Graphs Today Graphs (Implementation, Traversal)

Preview:

Citation preview

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

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

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.

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

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

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

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

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

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

BFS Example

r s t u

v w x y

BFS Example

0

r s t u

v w x y

sQ:

BFS Example

1

0

1

r s t u

v w x y

wQ: r

BFS Example

1

0

1

2

2

r s t u

v w x y

rQ: t x

BFS Example

1

2

0

1

2

2

r s t u

v w x y

Q: t x v

BFS Example

1

2

0

1

2

2

3

r s t u

v w x y

Q: x v u

BFS Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: v u y

BFS Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: u y

BFS Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: y

BFS Example

1

2

0

1

2

2

3

3

r s t u

v w x y

Q: Ø

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