Raphs: - Definitions - The Graph ADT - Data Structures For Graphs
Raphs: - Definitions - The Graph ADT - Data Structures For Graphs
Raphs: - Definitions - The Graph ADT - Data Structures For Graphs
Graphs
What is a Graph?
A graph G = (V,E) is composed of: V: set of vertices E: set of edges connecting the vertices in V An edge e = (u,v) is a pair of vertices Example: V= {a,b,c,d,e} a c d e b E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)}
Graphs
Applications
electronic circuits CS16
start nd the path of least resistance to CS16 networks (roads, ights, communications)
Graphs
mo better examples
A Spike Lee Joint Production
scheduling (project planning) wake up cs16 meditation work play cs16 program cxhextris make cookies for cs16 HTA sleep dream of cs16 A typical student day eat
more cs16
Graphs
Graph Terminology
adjacent vertices: connected by an edge degree (of a vertex): # of adjacent vertices 3 2
3 3 3
Since adjacent vertices each count the adjoining edge, it will be counted twice
path: sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent. a c d abedc e d bedc b a c e b
Graphs
cycle: simple path, except that the last vertex is the same as the rst vertex a c d e b acda
Graphs
connected
not connected
subgraph: subset of vertices and edges forming a graph connected component: maximal connected subgraph. E.g., the graph below has 3 connected components.
Graphs
tree
tree
Graphs
Connectivity
Let n = #vertices m = #edges - complete graph - all pairs of vertices are adjacent m= (1/2) deg(v) = (1/2) (n - 1) = n(n-1)/2
vV vV
Each of the n vertices is incident to n - 1 edges, however, we would have counted each edge twice!!! Therefore, intuitively, m = n(n-1)/2.
n=5 m = (5 4)/2 = 10
Graphs
More Connectivity
n = #vertices m = #edges For a tree m = n - 1 n=5 m=4
Graphs
10
Spanning Tree
A spanning tree of G is a subgraph which - is a tree - contains all vertices of G
spanning tree of G
Graphs
11
Roberto wants to call the TAs to suggest an extension for the next program...
One fault will disconnect part of graph!! A cycle would be more fault tolerant and only requires n edges
Graphs
12
Gilligans Isle?
C A B D
Pregal River
Can one walk across each bridge exactly once and return at the starting point? Consider if you were a UPS driver, and you didnt want to retrace your steps. In 1736, Euler proved that this is not possible
Graphs
13
C A B
Eulerian Tour: path that traverses every edge exactly once and returns to the rst vertex Eulers Theorem: A graph has a Eulerian Tour if and only if all vertices have even degree Do you nd such ideas interesting? Would you enjoy spending a whole semester doing such proofs?
Notation: Graph G; Vertices v, w; Edge e; Object o - numVertices() Return the number of vertices of G. - numEdges() Return the number of edges of G. - vertices() Return an enumeration of the vertices of G. - edges() Return an enumeration of the edges of G.
Graphs
15
Update Methods
- makeUndirected(e) Set e to be an undirected edge. - reverseDirection(e) Switch the origin and destination vertices of e. - setDirectionFrom(e, v) Sets the direction of e away from v, one of its end vertices. - setDirectionTo(e, v) Sets the direction of e toward v, one of its end vertices. - insertEdge(v, w, o) Insert and return an undirected edge between v and w, storing o at this position. - insertDirectedEdge(v, w, o) Insert and return a directed edge between v and w, storing o at this position. - insertVertex(o) Insert and return a new (isolated) vertex storing o at this position. - removeEdge(e) Remove edge e.
Graphs 18
DL 3
AA 903
MIA
SFO
A U
AA
LAX
AA 49
DFW
AA
AA 411
523
Additional structures can be used to perform efciently the methods of the Graph ADT
Graphs
DL
19
8 13
247
20 1
UA 8 77
35
35
Edge List
The edge list structure simply stores the vertices and the edges into unsorted sequences. Easy to implement. Finding the edges incident on a given vertex is inefcient since it requires examining the entire edge sequence
E
NW 35 DL 247 AA 49 DL 335 AA 1387 AA 523 AA 411 UA 120 AA 903 UA 877 TW 45
BOS
LAX
DFW
JFK
MIA
ORD
SFO
Graphs
20
O(1)
O(m)
Graphs
21
e b a a a b c e d c c e e d d
deg(v)) = (N + M)
22
NW 35 DL 247
AA 49
BOS
LAX
DFW
JFK
MIA
ORD
SFO
in
out
NW 35 DL 247
in
out
in
out
in
out
in
out
in
out
in
TW 45
out
AA 49 UA 120 AA 411
NW 35 AA1387 AA 903 TW 45
O(deg(v))
O(min(deg(u), deg(v))) insertVertex, insertEdge, insertDirected- O(1) Edge, removeEdge, makeUndirected, reverseDirection, removeVertex(v) O(deg(v))
Graphs 24
matrix M with entries for all pairs of vertices M[i,j] = true means that there is an edge (i,j) in the graph. M[i,j] = false means that there is no edge (i,j) in the graph. There is an entry for every possible edge, therefore: Space = (N2)
Graphs
25
1
AA 1387 AA 523 UA 877
2
NW 35
3
AA 49 AA 411
4
DL 247 AA 903
5
DL 335 UA 120
6
TW 45
BOS DFW JFK LAX MIA ORD SFO 0 1 2 3 4 5 6 The space requirement is O(n2 + m)
Graphs 26
O(1) O(1)
O(n2)
Graphs
27