Graphs 337 LAX Graphs 3 4 7 1

  • Slides: 14
Download presentation
Graphs 337 LAX Graphs 3 4 7 1 1233 ORD 802 SFO 1843 DFW

Graphs 337 LAX Graphs 3 4 7 1 1233 ORD 802 SFO 1843 DFW 1

Outline and Reading Graphs (§ 6. 1) n n n Definition Applications Terminology Properties

Outline and Reading Graphs (§ 6. 1) n n n Definition Applications Terminology Properties ADT Data structures for graphs (§ 6. 2) n n n Edge list structure Adjacency matrix structure Graphs 2

Graph A graph is a pair (V, E), where n n n V is

Graph A graph is a pair (V, E), where n n n V is a set of nodes, called vertices E is a collection of pairs of vertices, called edges Vertices and edges are positions and store elements Example: n A vertex represents an airport and stores the three-letter airport code An edge represents a flight route between two airports and stores the mileage of the route SFO 337 HNL 2555 LAX Graphs 1843 3 4 17 1233 849 ORD 802 n DFW 2 PVD 14 7 8 3 1 LGA 1120 10 99 MIA 3

Edge Types Directed edge n n ordered pair of vertices (u, v) first vertex

Edge Types Directed edge n n ordered pair of vertices (u, v) first vertex u is the origin second vertex v is the destination e. g. , a flight ORD flight AA 1206 PVD ORD 849 miles PVD Undirected edge n unordered pair of vertices (u, v) e. g. , a flight route Directed graph n n all the edges are directed e. g. , route network Undirected graph n n all the edges are undirected e. g. , flight network Graphs 4

Applications Electronic circuits n n Printed circuit board Integrated circuit Transportation networks n n

Applications Electronic circuits n n Printed circuit board Integrated circuit Transportation networks n n Highway network Flight network Computer networks n n n Local area network Internet Web Databases n Entity-relationship diagram Graphs 5

Terminology End vertices (or endpoints) of an edge n U and V are the

Terminology End vertices (or endpoints) of an edge n U and V are the endpoints of a a Edges incident on a vertex n a, d, and b are incident on V Adjacent vertices n U and V are adjacent Degree of a vertex n h and i are parallel edges Self-loop n b d U h X c e W X has degree 5 Parallel edges n V j Z i g f Y j is a self-loop Graphs 6

Terminology (cont. ) Path n n sequence of alternating vertices and edges begins with

Terminology (cont. ) Path n n sequence of alternating vertices and edges begins with a vertex ends with a vertex each edge is preceded and followed by its endpoints Simple path n path such that all its vertices and edges are distinct Examples n n 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 Graphs a U c V b d P 2 P 1 X e W h Z g f Y 7

Terminology (cont. ) Cycle n n circular sequence of alternating vertices and edges each

Terminology (cont. ) Cycle n n circular sequence of alternating vertices and edges each edge is preceded and followed by its endpoints Simple cycle n cycle such that all its vertices and edges are distinct Examples n n C 1=(V, b, X, g, Y, f, W, c, U, a, ) is a simple cycle C 2=(U, c, W, e, X, g, Y, f, W, d, V, a, ) is a cycle that is not simple Graphs a U c V b d C 2 X e C 1 g W f h Z Y 8

Properties Property 1 Notation Sv deg(v) = 2 m Proof: each edge is counted

Properties Property 1 Notation Sv deg(v) = 2 m Proof: each edge is counted twice Property 2 In an undirected graph with no self-loops and no multiple edges m n (n - 1)/2 Proof: each vertex has degree at most (n - 1) n m deg(v) number of vertices number of edges degree of vertex v Example n n = 4 n m = 6 n deg(v) = 3 What is the bound for a directed graph? Graphs 9

Main Methods of the Graph ADT Vertices and edges n n are positions store

Main Methods of the Graph ADT Vertices and edges n n are positions store elements n n Accessor methods n n n n Update methods a. Vertex() incident. Edges(v) end. Vertices(e) is. Directed(e) origin(e) destination(e) opposite(v, e) are. Adjacent(v, w) Graphs n n n insert. Vertex(o) insert. Edge(v, w, o) insert. Directed. Edge(v, w, o) remove. Vertex(v) remove. Edge(e) Generic methods n n num. Vertices() num. Edges() vertices() edges() 10

Edge List Structure u Vertex object n n a element reference to position in

Edge List Structure u Vertex object n n a element reference to position in vertex sequence v c b d w z Edge object n n element origin vertex object destination vertex object reference to position in edge sequence u z w v Vertex sequence n sequence of vertex objects a b c d Edge sequence n sequence of edge objects Graphs 11

Adjacency List Structure a Edge list structure Incidence sequence for each vertex n sequence

Adjacency List Structure a Edge list structure Incidence sequence for each vertex n sequence of references to edge objects of incident edges Augmented edge objects n references to associated positions in incidence sequences of end vertices Graphs v b u u w v a w b 12

Adjacency Matrix Structure a Edge list structure Augmented vertex objects n n b u

Adjacency Matrix Structure a Edge list structure Augmented vertex objects n n b u w Integer key (index) associated with vertex 2 D-array adjacency array n v 0 u 1 Reference to edge object for adjacent vertices Null for nonadjacent vertices The “old fashioned” version just has 0 for no edge and 1 for edge Graphs 0 0 2 1 w 2 1 a 2 v b 13

Asymptotic Performance n vertices, m edges no parallel edges no self-loops Bounds are “big-Oh”

Asymptotic Performance n vertices, m edges no parallel edges no self-loops Bounds are “big-Oh” Edge List Adjacency Matrix Space incident. Edges(v) are. Adjacent (v, w) insert. Vertex(o) n+m n 2 m m 1 deg(v) min(deg(v), deg(w)) 1 n 2 insert. Edge(v, w, o) 1 1 1 remove. Vertex(v) remove. Edge(e) m 1 deg(v) 1 n 2 1 Graphs 14