Title: ECE 242 Spring 2003 Data Structures in Java
1ECE 242 Spring 2003Data Structures in Java
- http//rio.ecs.umass.edu/ece242
- Concepts of Graphs and Graph Traversal
- Prof. Lixin Gao
2Todays Topics
- Graphs
- Definition
- Directed and Undirected graph
- Simple path and cycle
- Connected and Unconnected graph
- Weighted graph
- Graph Representation
- Graph Traversal
3Abstract Data Type
- We have discussed
- List
- Tree
- Today we will talk about Graph
4Northwest Airline Flight
Anchorage
Boston
Minneapolis
Seattle
Hartford
SF
Atlanta
Austin
5Course Prerequisites
Freshman
Sophomore
Junior
Senior
ECE 122
ECE 242
6Computer Network Or Internet
MCI
ATT
Regional Network
Campus Umass
Intel
7Application
- Salemans Route
- Postmans Route
- Shortest route for saleman, we call it as Travel
Saleman Problem
Start
8Interesting Problem
- Graph Traversal
- Shortest Path between two nodes
- Anything else?
9Concepts of Graphs
node or vertex
edges (weight)
10Graph Definition
- G (V, E)
- V is the vertex set
- Vertices are also called nodes or points
- E is the edge set
- Each edge connects two vertices
- Edges are also called arcs or lines
11Undirected vs. Directed Graph
- Undirected Graph
- no oriented edge
- Directed Graph
- every edge has oriented vertex
12Applications --- Communication Network
2
3
8
1
10
4
5
9
11
6
7
- Vertex city, edge communication link.
13Subgraph
- Subgraph
- subset of vertices and edges
14Simple Path
- A simple path traverse a node no more than once
- ABCD is a simple path
B
C
A
path
D
15Cycle
- A cycle is a path that starts and ends at the
same point - CBDC is a cycle
B
C
A
D
16Connected vs. Unconnected Graph
Connected Graph
Unconnected Graph
17Directed Acyclic Graph
- Directed Acyclic Graph (DAG) directed graph
without cycle - Examples
- Course Requirement Graph DAG
18Directed Acyclic Graph
- This is not DAG
- ABCD is a cycle
D
C
A
B
19Weighted Graph
- Weighted graph a graph with numbers assigned to
its edges - Weight cost, distance, travel time, hop, etc.
0
10
20
1
1
2
4
5
3
20Representation Of Graph
- Two representations
- Adjacency Matrix
- Adjacency List
21Adjacency Matrix
- N nodes in graph
- Use Matrix A0N-10N-1
- if vertex i and vertex j are adjacent in graph,
Aij 1, - otherwise Aij 0
- if vertex i has a loop, Aii 1
- if vertex i has no loop, Aii 0
22Example of Adjacency Matrix
0
1
2
3
0 1 1 0 1 0 1 1 1 1
0 1 0 1 1 0
So, Matrix A
23Undirected vs. Directed
- Undirected graph
- adjacency matrix is symmetric
- Aij always equals Aji
- Directed graph
- adjacency matrix may not be symmetric
- Aij may not equal Aji
24Directed Graph Matrix Representation
0
1
2
3
0 1 1 1 0 0 0 1 0 0
0 1 0 0 0 0
So, Matrix A
25Weighted Graph
0
10
20
2
1
1
0 20 10 1 20 0 0 5 10
0 0 4 1 5 4 0
4
5
3
So, Matrix A
26Adjacency List
- An array of list
- the ith element of array is a list of vertices
that connect to vertex i
27Example of Adjacency List
0
1
2
3
vertex 0 connect to vertex 1, 2 and 3 vertex 1
connects to 3 vertex 2 connects to 3
28Weighted Graph
- Weighted graph extend each node with an addition
field weight
0
1
10
2
20
3
1
0
10
20
1
0
10
3
4
1
1
2
2
0
20
3
5
4
3
5
0
1
1
4
2
5
3
29Compare Two Representations
- Given two vertices u, v
- find out if u and v are adjacent
- Given a vertex u
- enumerate all neighbors of u
- For all vertices
- enumerate all neighbors of each vertex
30Comparison Of Representations
31Complete Graph
- There is an edge between any two vertices
Total number of edges in graph E N(N-1)/2
O(N2)
32Sparse Graph
- There are very small no. of edges in the graph
33Space Requirement of Representations
- Memory space
- adjacency matrix O(N2)
- adjacency list O(E)
- Sparse graph
- adjacency list is better
- Dense graph
- same running time
34Graph Traversal
- List out all cities that United Airline can reach
from Hartford Airport
CHI
Hartford
SF
NYC
LA
W. DC
35Graph Traversal
- From vertex u, list out all vertices that u can
reach via graph G - Set of nodes to expand
- Each node has a flag to indicate visited or not
36Demos For Traversal Algorithm
- Step 1 Hartford
- find neighbors of Hartford
- Hartford, NYC, CHI
CHI
Hartford
SF
NYC
LA
W. DC
37Demos For Traversal Algorithm
- Step 2 Hartford, NYC, CHI
- find neighbors of NYC, CHI
- Hartford, NYC, CHI, LA, SF
CHI
Hartford
SF
NYC
LA
W. DC
38Demos For Traversal Algorithm
- Step 3 Hartford, NYC, CHI, LA, SF
- find neighbors of LA, SF
- no other new neighbors
CHI
Hartford
SF
NYC
LA
W. DC
39Demos For Traversal Algorithm
- Finally we get all cities that United Airline can
reach from Hartford Airport - Hartford, NYC, CHI, LA, SF
CHI
Hartford
SF
NYC
LA
W. DC
40Algorithm of Graph Traversal
- Mark all nodes as unvisited
- Pick a starting vertex u, add u to probing list
- While ( probing list is not empty)
-
- Remove a node v from probing list
- Mark node v as visited
- For each neighbor w of v, if w is unvisited,
add w to the probing list -
41Graph Traversal Algorithms
- Two algorithms
- Depth First Traversal
- Breadth First Traversal
42Depth First Traversal
- Probing List is implemented as stack (LIFO)
- Example
- As neighbor B, C, E
- Bs neighbor A, C, F
- Cs neighbor A, B, D
- Ds neighbor E, C, F
- Es neighbor A, D
- Fs neighbor B, D
- start from vertex A
A
B
E
C
D
F
43Depth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Initial State
- Visited Vertices
- Probing Vertices A
- Unvisited Vertices A, B, C, D, E, F
stack
A
44Depth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Peek a vertex from stack, it is A, mark it as
visited - Find As first unvisited neighbor, push it into
stack - Visited Vertices A
- Probing vertices A, B
- Unvisited Vertices B, C, D, E, F
B
A
A
stack
45Depth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Peek a vertex from stack, it is B, mark it as
visited - Find Bs first unvisited neighbor, push it in
stack - Visited Vertices A, B
- Probing Vertices A, B, C
- Unvisited Vertices C, D, E, F
C
B
B
A
A
stack
46Depth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Peek a vertex from stack, it is C, mark it as
visited - Find Cs first unvisited neighbor, push it in
stack - Visited Vertices A, B, C
- Probing Vertices A, B, C, D
- Unvisited Vertices D, E, F
D
C
C
B
B
A
A
stack
47Depth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Peek a vertex from stack, it is D, mark it as
visited - Find Ds first unvisited neighbor, push it in
stack - Visited Vertices A, B, C, D
- Probing Vertices A, B, C, D, E
- Unvisited Vertices E, F
E
D
D
C
C
B
B
A
A
stack
48Depth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Peek a vertex from stack, it is E, mark it as
visited - Find Es first unvisited neighbor, no vertex
found, Pop E - Visited Vertices A, B, C, D, E
- Probing Vertices A, B, C, D
- Unvisited Vertices F
E
D
D
C
C
B
B
A
A
stack
49Depth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Peek a vertex from stack, it is D, mark it as
visited - Find Ds first unvisited neighbor, push it in
stack - Visited Vertices A, B, C, D, E
- Probing Vertices A, B, C, D, F
- Unvisited Vertices F
F
D
D
C
C
B
B
A
A
stack
50Depth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Peek a vertex from stack, it is F, mark it as
visited - Find Fs first unvisited neighbor, no vertex
found, Pop F - Visited Vertices A, B, C, D, E, F
- Probing Vertices A, B, C, D
- Unvisited Vertices
F
D
D
C
C
B
B
A
A
stack
51Depth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Peek a vertex from stack, it is D, mark it as
visited - Find Ds first unvisited neighbor, no vertex
found, Pop D - Visited Vertices A, B, C, D, E, F
- Probing Vertices A, B, C
- Unvisited Vertices
D
C
C
B
B
A
A
stack
52Depth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Peek a vertex from stack, it is C, mark it as
visited - Find Cs first unvisited neighbor, no vertex
found, Pop C - Visited Vertices A, B, C, D, E, F
- Probing Vertices A, B
- Unvisited Vertices
C
B
B
A
A
stack
53Depth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Peek a vertex from stack, it is B, mark it as
visited - Find Bs first unvisited neighbor, no vertex
found, Pop B - Visited Vertices A, B, C, D, E, F
- Probing Vertices A
- Unvisited Vertices
B
A
A
stack
54Depth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Peek a vertex from stack, it is A, mark it as
visited - Find As first unvisited neighbor, no vertex
found, Pop A - Visited Vertices A, B, C, D, E, F
- Probing Vertices
- Unvisited Vertices
A
stack
55Depth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Now probing list is empty
- End of Depth First Traversal
- Visited Vertices A, B, C, D, E, F
- Probing Vertices
- Unvisited Vertices
stack
56Breadth First Traversal
- Probing List is implemented as queue (FIFO)
- Example
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
- start from vertex A
A
B
E
C
D
F
57Breadth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Initial State
- Visited Vertices
- Probing Vertices A
- Unvisited Vertices A, B, C, D, E, F
A
queue
58Breadth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Delete first vertex from queue, it is A, mark it
as visited - Find As all unvisited neighbors, mark them as
visited, put them into queue - Visited Vertices A, B, C, E
- Probing Vertices B, C, E
- Unvisited Vertices D, F
A
B
E
C
queue
59Breadth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Delete first vertex from queue, it is B, mark it
as visited - Find Bs all unvisited neighbors, mark them as
visited, put them into queue - Visited Vertices A, B, C, E, F
- Probing Vertices C, E, F
- Unvisited Vertices D
B
E
C
C
F
E
queue
60Breadth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Delete first vertex from queue, it is C, mark it
as visited - Find Cs all unvisited neighbors, mark them as
visited, put them into queue - Visited Vertices A, B, C, E, F, D
- Probing Vertices E, F, D
- Unvisited Vertices
C
F
E
E
D
F
queue
61Breadth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Delete first vertex from queue, it is E, mark it
as visited - Find Es all unvisited neighbors, no vertex found
- Visited Vertices A, B, C, E, F, D
- Probing Vertices F, D
- Unvisited Vertices
E
D
F
F
D
queue
62Breadth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Delete first vertex from queue, it is F, mark it
as visited - Find Fs all unvisited neighbors, no vertex found
- Visited Vertices A, B, C, E, F, D
- Probing Vertices D
- Unvisited Vertices
F
D
D
queue
63Breadth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Delete first vertex from queue, it is D, mark it
as visited - Find Ds all unvisited neighbors, no vertex found
- Visited Vertices A, B, C, E, F, D
- Probing Vertices
- Unvisited Vertices
D
queue
64Breadth First Traversal (Cont)
- As neighbor B C E
- Bs neighbor A C F
- Cs neighbor A B D
- Ds neighbor E C F
- Es neighbor A D
- Fs neighbor B D
A
B
E
C
D
F
- Now the queue is empty
- End of Breadth First Traversal
- Visited Vertices A, B, C, E, F, D
- Probing Vertices
- Unvisited Vertices
queue
65Difference Between DFT BFT
- Depth First Traversal (DFT)
- order of visited A, B, C, D, E, F
- Breadth First Traversal (BFT)
- order of visited A, B, C, E, F, D
A
B
E
C
D
F
66Complexity of Graph Traversal
- Adjacency Matrix
- Cost O(NN) O(N2)
- Adjacency List
- For each node v, degree of node v
- Sum of degree(v) 2E
- Cost O(E)
67Application of Graph Traversal
- Find out all connected components of a graph
- For each node v of the graph,
- if ( v does not belong to any existing component
) - depth-first traversal (v)
- all nodes from v form a connected component