l19 graphs
TRANSCRIPT
-
8/15/2019 L19 Graphs
1/36
-
8/15/2019 L19 Graphs
2/36
Graphs [email protected] 2
hat is a Graph!
A graph G " # $%&' is co(posed of)$ ) set of *ertices&) set of edges connecting the *ertices in $
An edge e " #u%*' is a pair of *ertices&xa(ple)
$" +a%,%c%d%e-&"+#a%,'%#a%c'%#a%d'%#,%e'%#c%d'%#c%e'%#d%e'-
-
8/15/2019 L19 Graphs
3/36
Graphs [email protected]
&lectronic circuits
/ind the path of least resistance to 0S1
Applications # '
-
8/15/2019 L19 Graphs
4/36
Graphs [email protected]
3et4orks #roads% flights% co((unications'
Applications # '
-
8/15/2019 L19 Graphs
5/36
Graphs [email protected] 5
Scheduling #pro6ect planning'
(o7 ,etter exa(plesA Spike 8ee 9oint Production
-
8/15/2019 L19 Graphs
6/36
Graphs [email protected]
Ad6acent *ertices ) connected ,: an edgeDegree #of a *ertex ') ; of ad6acent *ertices
< deg#*' " 2#; edges'Since ad6acent *ertice each count the ad6oining edge% it 4ill ,e countedt4ice
Graph Ter(inolog: # '
-
8/15/2019 L19 Graphs
7/36Graphs [email protected] =
Path ) se>uence of *ertices *1%*2%. . .*k such that consecuti*e *ertices * iand * i?1 are ad6acent.
Graph Ter(inolog: # '
-
8/15/2019 L19 Graphs
8/36Graphs [email protected]
Si(ple path) no repeated *ertices
ore Graph Ter(inolog: # '
-
8/15/2019 L19 Graphs
9/36Graphs [email protected] B
0:cle) si(ple path% except that the last *ertex is the sa(e as the first*ertex
ore Graph Ter(inolog: # '
-
8/15/2019 L19 Graphs
10/36Graphs [email protected] 1C
0onnected graph ) an: t4o *ertices are connected ,: so(e path
Su,graph ) su,set of *ertices and edges for(ing a graph0onnected co(ponent ) (axi(al connected su,graph. &.g.% the graph,elo4 has connected co(ponents.
&*en ore Ter(inolog: # '
-
8/15/2019 L19 Graphs
11/36Graphs [email protected] 11
&*en ore Ter(inolog: # '
-
8/15/2019 L19 Graphs
12/36Graphs [email protected] 12
#free' tree connected graph 4ithout c:cles/orest collection of trees
E0ara(,aF Another Ter(inolog: SlideF
-
8/15/2019 L19 Graphs
13/36Graphs [email protected] 1
8et n " ;*ertices( " ;edges
0o(plete graph all pairs of *ertices are ad6acent( " #1 2'< deg#*' " #1 2'
-
8/15/2019 L19 Graphs
14/36
Graphs [email protected] 1
n " ;*ertices( " ;edges/or a tree ( " n 1
ore 0onnecti*it: # '
-
8/15/2019 L19 Graphs
15/36
Graphs [email protected] 15
If ( n 1% G is not connected
ore 0onnecti*it: # '
-
8/15/2019 L19 Graphs
16/36
Graphs [email protected] 1
A spanning tree of G is a su,graph 4hichIs a tree0ontains all *ertices of G
/ailure on an: edge disconnects s:ste( #least fault tolerant'
Spanning Tree
-
8/15/2019 L19 Graphs
17/36
Graphs [email protected] 1=
Ro,erto 4ants to call the TA7s to suggest an extension for the nextprogra(...
Jne fault 4ill disconnect part of graphFF A c:cle 4ould ,e (ore fault tolerant and onl: re>uires n edges
ATKT *s. RTKT#Ro,erto Ta(assia K Telephone'
-
8/15/2019 L19 Graphs
18/36
Graphs [email protected] 1
&uler and the Lridges of Moenigs,erg
0an one 4alk across each ,ridge exactl: once and return at the starting point!
0onsider if :ou 4ere a NPS dri*er% and :ou didn7t 4ant to retrace :our steps.In 1= % &uler pro*ed that this is not possi,le
-
8/15/2019 L19 Graphs
19/36
Graphs [email protected] 1B
Graph odel#4ith parallel edges'
&ulerian Tour ) path that tra*erses e*er: edge exactl: once and returns to thefirst *ertex&uler7s Theore( ) A graph has a &ulerian Tour if and onl: if all *ertices ha*e
e*en degreeDo :ou find such ideas interesting!ould :ou en6o: spending a 4hole se(ester doing such proofs!
Well, look into CS22!If :ou dare...
-
8/15/2019 L19 Graphs
20/36
Graphs [email protected] 2C
The Graph ADT is a positional container 4hose positions are the*ertices and the edges ofthe graph.
SiOe#' Return the nu(,er of *ertices plus the nu(,er of edges of G.Is&(pt:#'&le(ents#'Positions#'S4ap#'Replace&le(ent#'
3otation ) Graph G $ertices *% 4 &dge e J,6ect o3u($ertices#'
Return the nu(,er of *ertices of G.3u(&dges#'
Return the nu(,er of edges of G.
$ertices #' Return an enu(eration of the *ertices of G.&dges #' Return an enu(eration of the edges of G.
The Graph ADT
-
8/15/2019 L19 Graphs
21/36
Graphs [email protected] 21
Directed&dges#'Return an enu(eration of all directed edges in G.
Nndirected&dges#' Return an enu(eration of all undirected edges in G.
Incident&dges#*'
Return an enu(eration of all edges incident on *.InIncident&dges#*'
Return an enu(eration of all the inco(ing edges to *.
JutIncident&dges#*' Return an enu(eration of all the outgoing edges fro( *.
The Graph ADT #contd.' # '
-
8/15/2019 L19 Graphs
22/36
Graphs [email protected] 22
Degree#*'Return the degree of *.
InDegree#*'Return the in degree of *.
JutDegree#*'
Return the out degree of *.
The Graph ADT #contd.' # '
-
8/15/2019 L19 Graphs
23/36
Graphs [email protected] 2
Ad6acent$ertices#*'Return an enu(eration of the *ertices ad6acent to *.
InAd6acent$ertices#*'Return an enu(eration of the *ertices ad6acent to * along inco(ing edges.
JutAd6acent$ertices#*'
Return an enu(eration of the *ertices ad6acent to * along outgoing edges. AreAd6acent#*%4'
Return 4hether *ertices * and 4 are ad6acent.
&nd$ertices#e'Return an arra: of siOe 2 storing the end *ertices of e.
Jrigin#e'Return the end *ertex fro( 4hich e lea*es.
ore ethods ... # '
-
8/15/2019 L19 Graphs
24/36
Graphs [email protected] 2
Destination#e'Return the end *ertex at 4hich e arri*es.
IsDirected#e'Return true iff e is directed
ore ethods ... # '
-
8/15/2019 L19 Graphs
25/36
Graphs [email protected] 25
akeNndirected#e'Set e to ,e an undirected edge.
Re*erseDirection#e'S4itch the origin and destination *ertices of e.
SetDirection/ro(#e% *'
Sets the direction of e a4a: fro( *% one of its end *ertices.SetDirectionTo#e% *'
Sets the direction of e to4ard *% one of its end *ertices.
Insert&dge#*% 4% o'Insert and return an undirected edge ,et4een * and 4% storing o at this position.
Npdate ethods # '
-
8/15/2019 L19 Graphs
26/36
Graphs [email protected] 2
InsertDirected&dge#*% 4% o'Insert and return a directed edge ,et4een * and 4% storing o at this position.
Insert$ertex#o'Insert and return a ne4 #isolated' *ertex storing o at this position.
Re(o*e&dge#e'
Re(o*e edge e.
Npdate ethods # '
-
8/15/2019 L19 Graphs
27/36
Graphs [email protected] 2=
A GraphF Ho4 can 4e represent it!To start 4ith% 4e store the *ertices and the edges into t4o containers% and4e store 4ith each edge o,6ect references to its end*ertices
Additional structures can ,e used to perfor( efficientl: the (ethods of the
Graph ADT
Data Structures for Graphs
-
8/15/2019 L19 Graphs
28/36
Graphs [email protected] 2
The edge list structure si(pl: stores the *ertices and the edges intounsorted se>uences.&as: to i(ple(ent./inding the edges incident on a gi*en *ertex is inefficient since itre>uires exa(ining the entire edge se>uence
&dge 8ist
-
8/15/2019 L19 Graphs
29/36
Graphs [email protected] 2B
Operation Time
SiOe% is&(pt:% replace&le(ent% s4ap J#1'3u($ertices% nu(&dges J#1'
$ertices J#n'
&dges% directed&dges% undirected&dges J#('
&le(ents% positions J#n?('&nd$ertices% opposite% origin% destinationisDirected% degree% inDegree% outDegree
J#1'
Incident&dges% inIncident&dges% outIncident&dges%ad6acent$ertices% inAd6acent$ertices%outAd6acent$ertices% areAd6acent
J#('
Insert$ertex% insert&dge% insertDirected&dge% re(o*e&dge% (akeNndirected%re*erseDirection% setDirection/ro(% setDirectionTo
J#1'
Re(o*e$ertex J#('
Perfor(ance of the &dge 8ist Structure
-
8/15/2019 L19 Graphs
30/36
Graphs [email protected] C
Ad6acenc: list of a *ertex * )Se>uence of *ertices ad6acent to *
Represent the graph ,: the ad6acenc: lists of all the *ertices
Ad6acenc: 8ist #traditional'
-
8/15/2019 L19 Graphs
31/36
Graphs [email protected] 1
The ad6acenc: list structure extends the edge list structure ,: addingincidence containers to each *ertex.
The space re>uire(ent is J#n ? ('.
Ad6acenc: 8ist #(odern'
P f ( f h Ad6
-
8/15/2019 L19 Graphs
32/36
Graphs [email protected] 2
Operation Time
SiOe% is&(pt:% replace&le(ent% s4ap J#1'3u($ertices% nu(&dges J#1'
$ertices J#n'
&dges% directed&dges% undirected&dges J#('
&le(ents% positions J#n?('
&nd$ertices% opposite% origin% destination isDirected%degree% inDegree% outDegree
J#1'
Incident&dges#*'% inIncident&dges#*'%outIncident&dges#*'% ad6acent$ertices#*'% inAd6acent$ertices#*'% outAd6acent$ertices#*'
J#deg#*''
AreAd6acent#u% *' J#(in#deg#u'%deg#*'''
insert$ertex% insert&dge% insertDirected&dge% re(o*e&dge% (akeNndirected% re*erseDirection%
J#1'
Re(o*e$ertex#*' J#deg#*''
Perfor(ance of the Ad6acenc:8ist Structure
-
8/15/2019 L19 Graphs
33/36
Graphs [email protected]
Ad6acenc: atrix #traditional'
atrix 4ith entries for all pairs of *erticesQi%6 " true (eans that there is an edge #i%6' in the graph.Qi%6 " false (eans that there is no edge #i%6' in the graph.There is an entr: for e*er: possi,le edge% therefore)
-
8/15/2019 L19 Graphs
34/36
Graphs [email protected]
The ad6acenc: (atrix structures aug(ents the edge list structure 4ith a(atrix 4here each ro4 and colu(n corresponds to a *ertex.
Ad6acenc: atrix #(odern' # '
-
8/15/2019 L19 Graphs
35/36
Graphs [email protected] 5
Ad6acenc: atrix #(odern' # '
The space re>uire(ent is J#n 2 ? ('
Perfor( nce of the Ad6 cenc
-
8/15/2019 L19 Graphs
36/36
Operation Time
SiOe% is&(pt:% replace&le(ent% s4ap J#1'3u($ertices% nu(&dges J#1'
$ertices J#n'
&dges% directed&dges% undirected&dges J#('
&le(ents% positions J#n?('
&nd$ertices% opposite% origin% destination isDirected%degree% inDegree% outDegree
J#1'
Incident&dges% inIncident&dges% outIncident&dges%ad6acent$ertices% inAd6acent$ertices%outAd6acent$ertices%
J#n'
Are Ad6acent J#1'
Insert&dge% insertDirected&dge% re(o*e&dge%(akeNndirected% re*erseDirection% setDirection/ro(%setDirectionTo
J#1'
Insert$ertex% re(o*e$ertex J#n 2'
Perfor(ance of the Ad6acenc:atrix Structure