8/18/2019 Chapter 9 data structure slide
1/38
GraphsData Structures
Dr. Fares saab 1
8/18/2019 Chapter 9 data structure slide
2/38
8/18/2019 Chapter 9 data structure slide
3/38
Formal definition of graphs
A graph G is defined as follows:
G=(V,E)
V(G): a finite nonempt! set of verticesE(G): a set of edges (pairs of vertices)
Dr. Fares saab 3
8/18/2019 Chapter 9 data structure slide
4/38
Directed vs" undirected
graphs When the edges in a graph have no direction the graph is called
undirected
Dr. Fares saab 4
8/18/2019 Chapter 9 data structure slide
5/38
Directed vs" undirected
graphs (cont") When the edges in a graph have a direction the graph is called
directed (or digraph)
E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)
Warning : if the graph is
directed, the order of the
vertices in each edge is
important !!
Dr. Fares saab 5
8/18/2019 Chapter 9 data structure slide
6/38
Trees vs graphs
Trees are special cases of graphs##
Dr. Fares saab 6
8/18/2019 Chapter 9 data structure slide
7/38
$raph terminolog!
Ad%acent nodes: two nodes are ad%acent if the! are connected b! an
edge
&ath: a se'uence of vertices that connect two nodes in a graph omplete graph: a graph in which ever! verte is directl! connected
to ever! other verte
5 is adjacen ! 77 is adjacen "r!# 5
Dr. Fares saab 7
8/18/2019 Chapter 9 data structure slide
8/38
$raph terminolog! (cont")
What is the number of edges in acomplete directed graph with * vertices?
N * (N-1)
2( )O N
Dr. Fares saab $
8/18/2019 Chapter 9 data structure slide
9/38
$raph terminolog! (cont")
Weighted graph: a graph in which each edge
carries a value
Dr. Fares saab 9
8/18/2019 Chapter 9 data structure slide
10/38
$raph implementation
Arra!,based implementation
A -D arra! is used to represent the vertices
A .D arra! (ad%acenc! matri) is used to represent the edges
Dr. Fares saab 1%
8/18/2019 Chapter 9 data structure slide
11/38
Arra!,based implementation
Dr. Fares saab 11
8/18/2019 Chapter 9 data structure slide
12/38
$raph implementation (cont")
/in0ed,list implementation
A -D arra! is used to represent the vertices
A list is used for each verte v which contains
the vertices which are ad%acent from v(ad%acenc! list)
Dr. Fares saab 12
8/18/2019 Chapter 9 data structure slide
13/38
/in0ed,list implementation
Dr. Fares saab 13
8/18/2019 Chapter 9 data structure slide
14/38
Ad%acenc! matri vs"ad%acenc! list representation
Adjacency matrix
onnectivit! between two vertices can be tested 'uic0l!
Adjacency list
1ertices ad%acent to another verte can be found 'uic0l!
Dr. Fares saab 14
8/18/2019 Chapter 9 data structure slide
15/38
$raph specification based onad%acenc! matri representationc!ns in &'EDGE = %*
e#p+aec+ass -ere/0pec+ass Graph/0pe { pb+ic
Graph/0pe(in)* Graph/0pe()* !id aeE#p0()* b!!+ 8sE#p0() c!ns* b!!+ 8sF++() c!ns* !id dd-ere(-ere/0pe)*
!id ddEd:e(-ere/0pe, -ere/0pe, in)* in ;ei:h8s(-ere/0pe, -ere/0pe)* !id Ge/!-erices(-ere/0pe, +earars()* !id ar-ere(-ere/0pe)* b!!+ 8sared(-ere/0pe) c!ns*
priae in n#-erices* in #a-erices* -ere/0pe? erices*
in ??ed:es* b!!+? #ars*@*
(c!nines)Dr. Fares saab 15
8/18/2019 Chapter 9 data structure slide
16/38
e#p+aec+ass -ere/0pe
Graph/0pe-ere/0peGraph/0pe(in #a-)
{
n#-erices = %* #a-erices = #a-*
erices = neA -ere/0peB#a-C*
ed:es = neA inB#a-C*
"!r(in i = %* i #a-* i)
ed:esBiC = neA inB#a-C*
#ars = neA b!!+B#a-C*@
e#p+aec+ass -ere/0pe
Graph/0pe-ere/0peGraph/0pe()
{
de+ee BC erices*
"!r(in i = %* i #a-erices* i) de+ee BC ed:esBiC*
de+ee BC ed:es*
de+ee BC #ars*
@
(c!nines)Dr. Fares saab 16
8/18/2019 Chapter 9 data structure slide
17/38
!id Graph/0pe-ere/0pe dd-ere(-ere/0pe ere){
ericesBn#-ericesC = ere*
"!r(in inde = %* inde n#-erices* inde) {
ed:esBn#-ericesCBindeC = &'EDGE*
ed:esBindeCBn#-ericesC = &'EDGE* @
n#-erices*@
e#p+aec+ass -ere/0pe
!id Graph/0pe-ere/0pe ddEd:e(-ere/0pe "r!#-ere,
-ere/0pe !-ere, in Aei:h){
in r!A*
in c!+#n*
r!A = 8nde8s(erices, "r!#-ere)*
c!+ = 8nde8s(erices, !-ere)*
ed:esBr!ACBc!+C = Aei:h*@
(c!nines)Dr. Fares saab 17
8/18/2019 Chapter 9 data structure slide
18/38
e#p+aec+ass -ere/0pe
in Graph/0pe-ere/0pe;ei:h8s(-ere/0pe "r!#-ere,-ere/0pe !-ere)
{
in r!A*
in c!+#n*
r!A = 8nde8s(erices, "r!#-ere)*
c!+ = 8nde8s(erices, !-ere)*
rern ed:esBr!ACBc!+C*
@
Dr. Fares saab 1$
8/18/2019 Chapter 9 data structure slide
19/38
$raphs as ADTs
ADT graph operations
Test whether graph is empt!"
$et number of vertices in a graph"
$et number of edges in a graph" See whether edge eists between two given vertices"
2nsert verte in graph whose vertices have distinct values that differ from new
verte3s value"
Data Structures and Problem Solving with C++: Walls and Mirrors, >arran! and enr0, 2%13
Dr. Fares saab
8/18/2019 Chapter 9 data structure slide
20/38
$raphs as ADTs
ADT graph operations ctd"
2nsert edge between two given vertices in graph"
4emove specified verte from graph and an! edges between the verte and other
vertices"
4emove edge between two vertices in graph"
4etrieve from graph verte that contains given value"
Data Structures and Problem Solving with C++: Walls and Mirrors, >arran! and enr0, 2%13
Dr. Fares saab
8/18/2019 Chapter 9 data structure slide
21/38
$raph searching
Proble: find a path between two nodes of the graph (e"g" Austin
and Washington) !ethods: Depth,First,Search (DFS) or 5readth,First,Search (5FS)
Dr. Fares saab 21
8/18/2019 Chapter 9 data structure slide
22/38
Depth,First,Search (DFS)
What is the idea behind DFS?
Travel as far as !ou can down a path
5ac0 up as little as possible when !ou reach a 6dead end6
(i"e" net verte has been 6mar0ed6 or there is no net
verte)
DFS can be implemented efficientl! using a
stac"
Dr. Fares saab 22
8/18/2019 Chapter 9 data structure slide
23/38
Depth,First,Search (DFS)
(cont#)e "!nd ! "a+sesac.Hsh(sar-ere)DI sac.H!p(ere)
8F ere == end-ere e "!nd ! re EE Hsh a++ adjacen erices !n! sac
;8E Jsac.8sE#p0() &D J"!nd 8F(J"!nd) ;rie KHah d!es n! eisK
Dr. Fares saab 23
8/18/2019 Chapter 9 data structure slide
24/38
sar end
(iniia+iLai!n)
Dr. Fares saab 24
8/18/2019 Chapter 9 data structure slide
25/38
Dr. Fares saab 25
8/18/2019 Chapter 9 data structure slide
26/38
Dr. Fares saab 26
8/18/2019 Chapter 9 data structure slide
27/38
e#p+ae c+ass 8e#/0pe
!id DephFirsearch(Graph/0pe-ere/0pe :raph, -ere/0pe sar-ere,-ere/0pe end-ere)
{
ac/0pe-ere/0pe sac*
8/18/2019 Chapter 9 data structure slide
28/38
e+se {
i"(J:raph.8sared(ere)) {
:raph.ar-ere(ere)* :raph.Ge/!-erices(ere, ere
8/18/2019 Chapter 9 data structure slide
29/38
e#p+aec+ass -ere/0pe
!id Graph/0pe-ere/0peGe/!-erices(-ere/0pe ere,
8/18/2019 Chapter 9 data structure slide
30/38
5readth,First,Searching (5FS)
What is the idea behind 5FS?
/oo0 at all possible paths at the same depth before !ou go at a deeper
level
5ac0 up as $ar as possible when !ou reach a 6dead end6 (i"e" net verte
has been 6mar0ed6 or there is no net verte)
Dr. Fares saab 3%
8/18/2019 Chapter 9 data structure slide
31/38
5readth,First,Searching (5FS) (cont")
5FS can be implemented efficientl! using a%ueue
e "!nd ! "a+se
Mee.EnMee(sar-ere)
DI Mee.DeMee(ere)
8F ere == end-ere
e "!nd ! re
EE
EnMee a++ adjacen erices !n! Mee
;8E JMee.8sE#p0() &D J"!nd
Should we mar0 a verte when it is en'ueued orwhen it is de'ueued ?
8F(J"!nd) ;rie KHah d!es n! eisK
Dr. Fares saab 31
8/18/2019 Chapter 9 data structure slide
32/38
sar end
(iniia+iLai!n)
Dr. Fares saab 32
8/18/2019 Chapter 9 data structure slide
33/38
ne
Dr. Fares saab 33
8/18/2019 Chapter 9 data structure slide
34/38
Dr. Fares saab 34
8/18/2019 Chapter 9 data structure slide
35/38
e#p+aec+ass -ere/0pe
!id NreadhFirsearch(Graph/0pe-ere/0pe :raph, -ere/0pesar-ere, -ere/0pe end-ere)*
{
8/18/2019 Chapter 9 data structure slide
36/38
e+se {
i"(J:raph.8sared(ere)) {
:raph.ar-ere(ere)*
:raph.Ge/!-erices(ere, ere
8/18/2019 Chapter 9 data structure slide
37/38
Single,source shortest,path problem
There are multiple paths from a source verte to a destination verte
&hortest path: the path whose total weight (i"e" sum of edge weights)is minimum
7amples:
Austin,89ouston,8Atlanta,8Washington: -;< miles
Austin,8Dallas,8Denver,8Atlanta,8Washington: .=>< miles
Dr. Fares saab 37
8/18/2019 Chapter 9 data structure slide
38/38
Single,source shortest,path problem(cont") ommon algorithms: 'i"stras algorithm ellan-+ord algorithm
5FS can be used to solve the shortest graph problem when the graphis weightlessweightless or all the weights are the same
(mar0 vertices before 7n'ueue)
Dr. Fares saab 3$