ECE 242 Spring 2003 Data Structures in Java - PowerPoint PPT Presentation

1 / 67
About This Presentation
Title:

ECE 242 Spring 2003 Data Structures in Java

Description:

http://rio.ecs.umass.edu/ece242. Concepts of Graphs and ... Minneapolis. Austin. SF. Seattle. Anchorage. ECE242 Spring 2003. Data Structures in Java, Prof. Gao ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 68
Provided by: jxia
Category:

less

Transcript and Presenter's Notes

Title: ECE 242 Spring 2003 Data Structures in Java


1
ECE 242 Spring 2003Data Structures in Java
  • http//rio.ecs.umass.edu/ece242
  • Concepts of Graphs and Graph Traversal
  • Prof. Lixin Gao

2
Todays Topics
  • Graphs
  • Definition
  • Directed and Undirected graph
  • Simple path and cycle
  • Connected and Unconnected graph
  • Weighted graph
  • Graph Representation
  • Graph Traversal

3
Abstract Data Type
  • We have discussed
  • List
  • Tree
  • Today we will talk about Graph

4
Northwest Airline Flight
Anchorage
Boston
Minneapolis
Seattle
Hartford
SF
Atlanta
Austin
5
Course Prerequisites
Freshman
Sophomore
Junior
Senior
ECE 122
ECE 242
6
Computer Network Or Internet
MCI
ATT
Regional Network
Campus Umass
Intel
7
Application
  • Salemans Route
  • Postmans Route
  • Shortest route for saleman, we call it as Travel
    Saleman Problem

Start
8
Interesting Problem
  • Graph Traversal
  • Shortest Path between two nodes
  • Anything else?

9
Concepts of Graphs
node or vertex
edges (weight)
10
Graph 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

11
Undirected vs. Directed Graph
  • Undirected Graph
  • no oriented edge
  • Directed Graph
  • every edge has oriented vertex

12
Applications --- Communication Network
2
3
8
1
10
4
5
9
11
6
7
  • Vertex city, edge communication link.

13
Subgraph
  • Subgraph
  • subset of vertices and edges

14
Simple Path
  • A simple path traverse a node no more than once
  • ABCD is a simple path

B
C
A
path
D
15
Cycle
  • A cycle is a path that starts and ends at the
    same point
  • CBDC is a cycle

B
C
A
D
16
Connected vs. Unconnected Graph
Connected Graph
Unconnected Graph
17
Directed Acyclic Graph
  • Directed Acyclic Graph (DAG) directed graph
    without cycle
  • Examples
  • Course Requirement Graph DAG

18
Directed Acyclic Graph
  • This is not DAG
  • ABCD is a cycle

D
C
A
B
19
Weighted 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
20
Representation Of Graph
  • Two representations
  • Adjacency Matrix
  • Adjacency List

21
Adjacency 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

22
Example 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
23
Undirected 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

24
Directed 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
25
Weighted 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
26
Adjacency List
  • An array of list
  • the ith element of array is a list of vertices
    that connect to vertex i

27
Example 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
28
Weighted 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
29
Compare 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

30
Comparison Of Representations
31
Complete Graph
  • There is an edge between any two vertices

Total number of edges in graph E N(N-1)/2
O(N2)
32
Sparse Graph
  • There are very small no. of edges in the graph
  • For example
  • E N-1 O(N)

33
Space Requirement of Representations
  • Memory space
  • adjacency matrix O(N2)
  • adjacency list O(E)
  • Sparse graph
  • adjacency list is better
  • Dense graph
  • same running time

34
Graph Traversal
  • List out all cities that United Airline can reach
    from Hartford Airport

CHI
Hartford
SF
NYC
LA
W. DC
35
Graph 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

36
Demos For Traversal Algorithm
  • Step 1 Hartford
  • find neighbors of Hartford
  • Hartford, NYC, CHI

CHI
Hartford
SF
NYC
LA
W. DC
37
Demos 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
38
Demos 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
39
Demos 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
40
Algorithm 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

41
Graph Traversal Algorithms
  • Two algorithms
  • Depth First Traversal
  • Breadth First Traversal

42
Depth 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
43
Depth 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
44
Depth 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
45
Depth 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
46
Depth 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
47
Depth 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
48
Depth 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
49
Depth 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
50
Depth 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
51
Depth 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
52
Depth 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
53
Depth 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
54
Depth 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
55
Depth 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
56
Breadth 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
57
Breadth 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
58
Breadth 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
59
Breadth 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
60
Breadth 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
61
Breadth 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
62
Breadth 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
63
Breadth 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
64
Breadth 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
65
Difference 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
66
Complexity 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)

67
Application 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
Write a Comment
User Comments (0)
About PowerShow.com