Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMore .
Lecture 27: Graph ADT
description
Transcript of Lecture 27: Graph ADT
![Page 1: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/1.jpg)
LECTURE 27:GRAPH ADT
CSC 213 – Large Scale Programming
![Page 2: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/2.jpg)
Today’s Goals
Discuss what is NOT meant by term “Graph” Why no bar charts, scatter plots, & pie charts
mentioned How term is used for mathematical graph
Review terminology of mathematical graphs Why do we care about edges & vertices Directed vs. undirected more than types of
plays Which 1 of these made up: incident, adjacent,
feeder Cycles & paths not related to Queen songs
Examine GRAPH ADT’s method & what they do
![Page 3: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/3.jpg)
Overview of Remainder of Term
![Page 4: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/4.jpg)
What We Are Not Discussing
![Page 5: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/5.jpg)
Graphs
Mathematically, graph is pair (V, E) where V is collection of nodes, called vertices Two nodes can be connected by an edge in
E Position implemented by Vertex & Edge
classes ORD PVD
MIADFW
SFO
LAX
LGAHNL
849
802
13871743
1843
10991120
1233337
2555
142
![Page 6: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/6.jpg)
Edge Types
Edge connects two vertices in the graph Image we have edge connecting u & v (u,v) is name given to this edge
![Page 7: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/7.jpg)
Edge Types
Edge connects two vertices in the graph Image we have edge connecting u & v (u,v) is name given to this edge
Edges can be directed One-way street is directed edge u is origin or source v is destination
Life Death
![Page 8: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/8.jpg)
Edge Types
Edge connects two vertices in the graph Image we have edge connecting u & v (u,v) is name given to this edge
Edges can be directed One-way street is directed edge u is origin or source v is destination
Undirected edge is alternative Vertices listed in any order Subway lines normally undirected
Life Death
Best Canisius
![Page 9: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/9.jpg)
Graph Types
Graph is directed if all edges directed All edges in graph must be directed Examples include trees & Java object
hierarchy Any edge allowed in undirected graphs
Can have only undirected or mix of both edges
Roadways & CSC major are examples of this
![Page 10: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/10.jpg)
John
DavidPaul
brown.edu
cox.net
cs.brown.edu
att.netqwest.net
math.brown.edu
cslab1bcslab1a
Applications
Electronic circuits Transportation networks Databases Packing suitcases Finding terrorists Scheduling college’s exams Assigning classes to rooms Garbage collection Coloring countries on a map Playing minesweeper
![Page 11: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/11.jpg)
Other Applications
![Page 12: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/12.jpg)
Terminology
Edge incident on its endpoints U & V endpoints of a
Vertices adjacent when joined by edge U adjacent to V V adjacent to U Directedness unimportant
determining adjacency
XU
V
W
Z
Y
a
c
b
e
d
f
g
h
i
j
![Page 13: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/13.jpg)
Terminology
Degree of vertex is number incident edges X has degree of 5 Does not matter if
edges directed or not What the edges’ other
endvertices unimportant (Self-edges only count once)
XU
V
W
Z
Y
a
c
b
e
d
f
g
h
i
j
![Page 14: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/14.jpg)
Path Terminology
Path is set of vertices in Graph where All of the consecutive vertices adjacent May or may not have edge from last to first
vertices
(U, W, X, Y, W, V) is pathXU
V
W
Z
Y
a
c
b
e
f
g
h
![Page 15: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/15.jpg)
Path Terminology
Simple path is a path which: Is named by alternating vertices & edges 0 or 1 appearance for each vertex & edge
(V, b, X, h, Z) is simple path XU
V
W
Z
Y
a
c
b
e
d
f
g
h
![Page 16: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/16.jpg)
Cycle Terminology
Cycle is path with several additional properties Each cycle must begin & end at same
vertex No edge required from this vertex to itself
Simple cycle is special cycle But is also simple path
(V, X, Y, W, U, V) is simple cycle
XU
V
W
Z
Y
a
c
b
e
d
f
g
h
![Page 17: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/17.jpg)
Graph ADT
Accessor methods vertices(): iterable for
vertices edges(): iterable for
edges endVertices(e): array
with end points of edge e
opposite(v,e): e’s end point that is not v
areAdjacent(v,w): check if v and w are adjacent
replace(v,x): make x new element at vertex v
replace(e,x): make x new element at edge e
Update methods insertVertex(x):
create vertex storing element x
insertEdge(v,w,x): add edge (v,w) with element x
removeVertex(v): remove v (& incident edges)
removeEdge(e): remove e
Retrieval methods incidentEdges(v): get
edges incident to v
![Page 18: Lecture 27: Graph ADT](https://reader035.fdocuments.us/reader035/viewer/2022062305/5681624c550346895dd2963d/html5/thumbnails/18.jpg)
For Next Lecture
New weekly assignment available now Usual time for when it is due: 5PM on
Tuesday Consider design & develop tests for
program #2 2nd deadline is Friday, so get cracking Spend time working on this: design saves
coding Reading on implementing Graph for
Friday What are simplest implementations of
Graph? When would each be good to use is
program?