Professional Documents
Culture Documents
National University
Page
One limitation of the trees is that the trees are hierarchal in nature. They only represent relations of hierarchal types such as relations between parents and child A generalization of tree, called GRAP , is a data structure in which these limitations are lifted A graph is a collection of !ertices "nodes# and connection between them called edges A simple graph G= (V,E) consists of a nonempty set of V of !ertices and possibly empty set E of edges where each edge is a connection between two !ertices The number of !ertices and edges of a graph are represented as | V| and |E|, respecti!ely.
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 2
Graph 1
Graph 2
Graph 3
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University
Graph 4
Page 3
National University
Page
%ulti&graphs
'n a simple graph, two !ertices can only be (oined by one edge owe!er, in a multi$graph more than one edge can connect the same two !ertices together.
A multi$graph that has an edge going from !erte& vi to the same !erte& vi "loop edge# is called pseudo$graph
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page $
(ath) A path from v1 to vn is a se)uence of edges edge(v1,v2 , edge(v2, v! , " (vn#1, vn *+cle) 'f v1 $ vn and no edge is repeated then it is called a cycle
-n the follo.ing graph, the e!ges that connects v1 to v2 an! v2 to v! an! v! to v% ma/es a path from v1 to v%0
,1 ,2 ,3
1he path going from v1 to v2 an! from v2 to v! an! from v! to v1 again ma/es a c+cle0
,1 ,2 ,3
,4 ,$
A.R. Hadaegh Dr. Ahmad R. Hadaegh
,4 ,$ ,'
,'
National University
Page
'
3eighte! Graph) A graph is called weighted graph if each edge has an assigned number *epending on the conte&t in which such a graphs are used, the number assigned to an edge is called its weight, cost, distance, etc. *omplete Graph) A graph with n !ertices is called +complete, and is denoted as &n if for each pair of distinct !ertices e&actly one edge connecting them. That is each !erte& can be connected to any other !erte& The number of edges in such a graph is|V| |E| = 2
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 2
/4
Su:&graph) A sub$graph '( of graph G=(V, E) is a graph G;=(V;, E;) such that V( is a subset of V and E( is a subset of E . A .ub$graph induced by !ertices V( is a graph "V(, E( such that if an edge e is in E(, then e is in E <!=acent) Two !ertices vi and vj are called ad(acent if the edge (vi,vj is in E. .uch an edge is called in)ident with the !ertices vi and vj egree) The degree of a !erte& v, deg(v is the number of edges incident with v. 'f deg(v $*, then v is called isolated !erte&.
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 9
Graph ?epresentation
Graph can be represented "implemented# in se!eral ways. A simple representation is gi!en by ad(acency list that specifies all !ertices ad(acent to each !erte& of a graph This is called +,AR re-resentation
a : a : c ! e f c ! e g f g
National University Page >
c ! a a : a
! e f : ! c
%atrix ?epresentation
Another common representation is a Ad(acency matri& An ad(acency matri& of graph G= (V, E) is a binary |V|5|V| matri& such that for each entry of this matri& <4i, =7 <4i, =7 = 1 if there e&ist an edge between !erte& vi and vj Otherwise <4i, =7 = @
a : a a 0 0 1 c ! e : 1 c 0 1 f g 0 !
National University
: 0 0 0 1 1 0 0
c 1 0 0 0 0 1 0
! 1 1 0 0 1 1 0
e 0 1 0 1 0 0 0
f 1 0 1 1 0 0 0
g 0 0 0 0 0 0 0
Page
1@
*omparison)
/hich representation is better0 Thin1 about searching for an edge Thin1 about printing all edges Thin1 about inserting an edge ow about inserting a !erte& ow about deleting a !erte& ow about deleting an edge
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 11
Graph 1ra,ersal
Two systematic method for tra!ersal of graph are depth$first tra!ersal and breadth$first tra!ersal Of course since graph may contain cycle, they ha!e different structure compare to trees Therefore, tra!ersal algorithms we used for trees cannot be applied for graphs e&actly the same way and they should be modified to fit the graph re)uirements 2urther, not all graphs are connected. A graph may contain isolated trees, isolated graphs or isolated !ertices. 'n a connected graph, we can find a path between any two !ertices in the graph but this is not the case for unconnected graphs
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 12
National University
Page
13
AS(,) num(,) = iCC 66 ma/ing ,ertex , ,isite! for all ,ertices u a!=acent to , if num(u) is @ 66 it means it is not ,isite! +et attach e!ge(u,) to e!ges AS(u)D !epthAirstSearch() for all ,ertices , num(,) = @D 66 initialiEing all ,ertices to un,isite! e!ges = nullD i = 1D .hile there is a ,ertex , such that num(,) is @ AS(,)D output e!gesD
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 14
3ote that this algorithm guarantees generating a tree "or a forest "combination of se!eral smaller trees# which includes or spawns o!er all !ertices of the original graph The tree that meets these condition is called a spanning tree Also note that an edge is added to edges only if the condition in +i. n/m(/ is *, is true4 that is if !erte& / reachable from !erte& v has not been processed Therefore, certain edges in the original graph do not appear in the resulting tree The edges included in the tree are called forward edges "or tree edges# and the ones not included are called back edges 'n the ne&t e&le, we show bac1 edges with dash lines and forward edges with straight line
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 1$
Original Graph
c f g h
a(1)
/e pic1 up a !erte& randomly and mar1 it as !isited 5ets pic1 !erte& a and mar1 it as !isited
i
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 1'
a(1)
The ad(acent !ertices to a are ), ., g and i Any of the ad(acent !ertices to a can be chosen randomly as long as it is not !isited already 5ets pic1 ). 6ar1 ) as !isited and add edge a) to the edges The ad(acent !ertices to ) are a, . and i 7erte& a cannot be chosen because it is already !isited. 8ut !erte& . or i can be chosen 5ets pic1 .. 6ar1 . as !isited and add edge ). to the edges
c(2)
a(1)
c(2)
f(3)
i
A.R. Hadaegh Dr. Ahmad R. Hadaegh
National University
Page
12
a(1)
The ad(acent !ertices to . are a, ) and i 7erte& a and ) cannot be chosen because they are already !isited. .o the only choice is !erte& i 6ar1 i as !isited and add edge .i to the edges The ad(acent !ertices to i are a, ) and . but they are all !isited
c(2)
f(3)
i(4)
a(1)
/e bac1trac1 to . and !ertices ad(acent to . are all !isited. /e bac1trac1 to ) and !ertices ad(acent to ) are also !isited /e bac1trac1 to a and we find out that g, an incident edge to a, is not !isited
c(2)
f(3)
g($)
i(4)
A.R. Hadaegh Dr. Ahmad R. Hadaegh
6ar1 g as !isited and add edge ag to the edges National University Page
19
a(1)
:(')
The ad(acent !ertices to g are a and 0 7erte& a cannot be chosen because it is already !isited. .o the only choice is !erte& 0 6ar1 0 as !isited and add edge g0 to the edges The ad(acent !ertices to 0 is g, but g is already !isited
c(2)
f(3)
g($)
i(4)
a(1)
:(')
e(2)
/e bac1trac1 to g and !ertices ad(acent to g are all !isited. /e bac1trac1 to a but all ad(acent !ertices to a are also !isited .ince a is where we started, we cannot bac1trac1 anymore, so we pic1 another un!isited !erte& if there is still one
c(2)
f(3)
g($)
i(4)
1>
a(1)
:(')
e(2)
The only ad(acent !erte& to e is h 6ar1 h as !isited and add edge eh to the edges
c(2)
f(3)
g($)
h(9)
i(4)
a(1)
:(')
e(2)
!(>)
The ad(acent !ertices to h are e and d 7erte& e cannot be chosen because it is already !isited. .o the only choice is !erte& d 6ar1 d as !isited and add edge hd to the edges
c(2)
f(3)
g($)
h(9)
i(4)
National University
Page
2@
The ad(acent !ertices to d is h, but h is already !isited /e bac1trac1 to h and ad(acent !ertices to h are all !isited. /e bac1trac1 to e and ad(acent !ertices to e are also !isited .ince e is where we started, we cannot bac1trac1 anymore, so we pic1 another un!isited !erte& if there is still one 8ut there is no more !erte& left to pic1. Therefore, we are done
A.R. Hadaegh Dr. Ahmad R. Hadaegh
a(1)
:(')
e(2)
!(>)
c(2)
f(3)
g($)
h(9)
c(2)
f(3)
g($)
h(9)
i(4)
National University
Page
21
Original Graph
c f g h
a(1)
/e pic1 up a !erte& randomly and mar1 it as !isited 5ets pic1 !erte& a and mar1 it as !isited
i
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 22
a(1)
The ad(acent !ertices to a are ), and . Any of the ad(acent !ertices to a can be chosen randomly as long as it is not !isited already 5ets pic1 ). 6ar1 ) as !isited and add edge a ) to the edges
c(2)
a(1)
The only ad(acent !ertices to ) is i 6ar1 i as !isited and add edge ) i to the edges
c(2)
i(3)
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 23
a(1)
The only ad(acent !erte& to i is a which is already !isited /e bac1trac1 to ) and the only !erte& ad(acent to ) is also !isited.
c(2)
f(4)
/e bac1trac1 to a and we find out that . is not !isited 6ar1 . as !isited and add edge a . to the edges
i(3)
a(1)
:($)
The ad(acent !ertices to . are ) and i which are already !isited /e bac1trac1 to a and all ad(acent !ertices to a are also !isited
c(2)
f(4)
i(3)
A.R. Hadaegh Dr. Ahmad R. Hadaegh
.ince we started with a, we cannot bac1trac1 anymore. .o we pic1 another un!isited !erte& if there e&ist one
National University
Page
24
a(1)
:($)
c(2)
f(4)
g(')
i(3)
a(1)
:($)
e(2)
The only ad(acent !erte& to g is a which is already !isited /e bac1trac1 to 0 and the ad(acent !erte& to 0 is also !isited
c(2)
f(4)
g(')
i(3)
A.R. Hadaegh Dr. Ahmad R. Hadaegh
.ince we started with 0, we cannot bac1trac1 anymore. .o we pic1 another un!isited !erte& if there e&ist one 5ets pic1 e and mar1 it as !isited
National University Page 2$
a(1)
:($)
e(2)
c(2)
f(4)
g(')
h(9)
i(3)
a(1)
:($)
e(2)
!(>)
c(2)
f(4)
g(')
h(9)
i(3)
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 2'
There is no ad(acent !erte& to d /e bac1trac1 to h and the ad(acent !erte& to h is already !isited. /e bac1trac1 to e and the ad(acent !erte& to e is also !isited .ince e is where we started, we cannot bac1trac1 anymore, so we pic1 another un!isited !erte& if there is still one 8ut there is no more !erte& left to pic1. Therefore, we are done
A.R. Hadaegh Dr. Ahmad R. Hadaegh
a(1)
:($)
e(2)
!(>)
c(2)
f(4)
g(')
h(9)
i(3)
e(2)
!(>)
c(2)
f(4)
g(')
h(9)
i(3)
National University Page 22
Frea!th&Airst Search
This type of tra!ersal uses )ueue instead of recursion or stac1
:reathAirstSearch() for all ,ertices u num(u) =@D e!ges = nullD i = 1D .hile there is a ,ertex such that num(,) = @ num(,) = iCCD enGueue(,)D 66 pushing into Gueue .hile Gueue is not empt+ , = !eGueue()D 66 popping from the Gueue for all ,ertices u a!=acent to , if num(u) is @ num(u) = iCCD enGueue(u)D a!! e!ge(,u) to e!gesD 8utput e!gesD
National University
Page
2>
Original Graph
c f g h
a(1)
/e pic1 up a !erte& randomly and mar1 it as !isited 5ets pic1 !erte& a and mar1 it as !isited and place it in the )ueue
Page 3@
i
A.R. Hadaegh Dr. Ahmad R. Hadaegh
a
National University
c(2)
f(4)
g($)
c i f g
The ad(acent !ertices to a are g, ., i and ) that are not !isited /e mar1 them all one by one and place them in the )ueue "order is not important# The related edges are added /e pop ) from the )ueue.
i(3)
a(1)
c(2)
f(4)
g($)
h i f g
National University
All ad(acent !ertices to ) are !isited already. .o no !erte& goes into the )ueue
Page 31
i(3)
a(1)
/e pop i from the )ueue. All ad(acent !ertices to i are !isited already.
f g
c(2)
f(4)
g($)
i(3)
a(1)
/e pop . from the )ueue. All ad(acent !ertices to . are !isited already.
c(2)
f(4)
g($)
i(3)
g
National University Page 32
a(1)
:(')
/e pop g from the )ueue. The only ad(acent !erte& to g that is not !isited is 0 /e mar1 0 and place it into the )ueue
c(2)
f(4)
g($)
i(3)
/e add edge g 0
a(1)
:(')
/e pop 0 from the )ueue but ad(acent !erte& to 0 is !isited already The )ueue become empty /e see if there is any un!isited !erte&
c(2)
f(4)
g($)
h(2)
i(3)
A.R. Hadaegh Dr. Ahmad R. Hadaegh
h
National University
a(1)
:(')
e(9)
!(>)
c(2)
f(4)
g($)
h(2) e !
/e mar1 them and place them in the )ueue /e also add the related edges
i(3)
a(1)
:(')
e(9)
!(>)
c(2)
f(4)
g($)
h(2)
i(3)
A.R. Hadaegh Dr. Ahmad R. Hadaegh
!
National University Page 34
/e pop d Ad(acent !ertices to d are all !isited already 3othing more is left to pop. 3o more un!isited !erte& e&ist Therefore, we are done
c(2) f(4)
a(1)
:(')
e(9)
!(>)
c(2)
f(4)
g($)
h(2)
i(3)
g($)
h(2)
i(3)
A.R. Hadaegh Dr. Ahmad R. Hadaegh National University Page 3$