Download presentation
Presentation is loading. Please wait.
1
CSC 213 ORD DFW SFO LAX 802 1743 1843 1233 337 Lecture 20: Graphs
2
Graphs (§ 12.1) A graph is a pair (V, E), where V is set of nodes, called vertices E is collection of edges, where each edge connects two nodes Vertices and edges implement Position ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142
3
Edge Types Directed edge ordered pair of vertices (u,v) u is the origin or source v is the destination e.g., a one-way street Undirected edge unordered pair of vertices (u,v) e.g., subway line Directed graph all graph edges are directed e.g., Java object hierarchy Undirected graph edges are undirected e.g., American highways ORDPVD flight AA 1206 ORDPVD 849 miles
4
Applications Electronic circuits Transportation networks Computer networks Databases Packing suitcases Finding terrorists Scheduling exams for a college Assigning classes to rooms Garbage collection Assigning colors to countries on a map Playing minesweeper
5
Terminology Edge incident on its endpoints U & V endpoints of a Vertices adjacent if joined by an edge U & V are adjacent Degree of vertex is number incident edges X has degree 5 XU V W Z Y a c b e d f g h i j
6
P1P1 Terminology (cont.) Path alternates vertices and edges begins at a vertex ends at a vertex edges preceded and followed by endpoints Simple path path that visits vertices and edges at most once Examples P 1 =(V,b,X,h,Z) is a simple path P 2 =(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple XU V W Z Y a c b e d f g hP2P2
7
Terminology (cont.) Cycle path that begins and ends at the same vertex Simple cycle simple path that is a cycle Examples C 1 =(V,b,X,g,Y,f,W,c, U,a, ) is simple cycle C1C1 XU V W Z Y a c b e d f g h
8
Main Methods of the Graph ADT Accessor methods endVertices(e): array with two endvertices of e opposite(v, e): endvertex of e that is not v areAdjacent(v, w): returns if v and w are adjacent replace(v, x): make x the new element at vertex v replace(e, x): make x the new element at edge e Update methods insertVertex(o): add new vertex storing element o insertEdge(v, w, o): add edge (v,w) that stores element o removeVertex(v): remove v (and its incident edges) removeEdge(e): remove e Retrieval methods incidentEdges(v): edges incident to v
9
Edge List Structure (§ 12.2.1) Vertex object element node in vertex sequence Edge object element origin vertex destination vertex node in edge sequence v u w ac b a z d uv w z bcd
10
Adjacency List Structure (§ 12.2.2) Incidence sequence for each vertex sequence containing all incident edges Augmented edge objects sequence of all nodes for this edge in the incidence sequence u v w ab a uv w b
11
Adjacency Matrix Structure (§ 12.2.3) Augmented vertex objects Rank associated with vertex 2D-array adjacency array Reference to edge object for adjacent vertices Null for nonadjacent vertices u v w ab 012 0 1 2 a uv w 01 2 b
12
Asymptotic Performance n vertices, m edges no parallel edges no self-loops Bounds are “big-Oh” Edge List Adjacency List Adjacency Matrix Space n m n2n2 incidentEdges( v ) mdeg(v)n areAdjacent ( v, w ) mmin(deg(v), deg(w))1 insertVertex( o ) 11n2n2 insertEdge( v, w, o ) 111 removeVertex( v ) mdeg(v)n2n2 removeEdge( e ) 111
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.