Graphs in c language

chappidi_saritha 8,938 views 25 slides Dec 20, 2012
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

For PG students


Slide Content

Graphs in ‘C’ Language
By
Dr. C. Saritha
Lecturer in Electronics
SSBN Degree College, Anantapur

Data Structure
•Datastructureisaparticularwayof
sortingandorganizingdataina
computer.Sothatitcanbeused
efficiently.
•Differentkindsofdatastructuresare
suitedtodifferentkindsofapplications.

Types of Data Structure
•LinearDataStructure:Adatastructure
issaidtobelineardatastructureifits
elementsformasequence.
Ex:Array,Stack,Queueandlinkedlist.
•Non-LinearDataStructure:Elementsin
anon-lineardatastructuredonotform
asequence.
Ex:TreeandGraph.

Types of data structure
Data Structures
Linear Non-Linear
Arrays Linked lists Stack Queue Trees Graphs

Trees
•Atreeishierarchicalcollectionof
nodes.Oneofthenodes,knownasthe
root,isatthetopofthehierarchical.
•Eachnodecanhaveutmostonelink
comingintoit.
•Thenodewherethelinkoriginatesis
calledtheparentnode.Therootnode
hasnoparent.

Continue…
•Thelinksleavinganode(anynumber
oflinksareallowed)pointtochild
nodes.
A
B C D
EFGHIJ

Graphs
•Agraphisasetofnodes(alsocalled
vertices)andasetofarcs(alsocalled
edges).Asamplegraphisasfallows:
A
D
C
B
E

Edges & Vertices
•Eachnodeinagraphisknownasa
vertexofthegraph.
•TheEachedgeofagraphcontainstwo
vertices.

Types of Graphs
•TheGraphscanbeclassifiedintotwo
types.Theyare:
Undirectedgraphs
Directedgraph

Continue…
Undirectedgraphs:Inanundirected
graphtheorderofpairisunimportant.
Hencethepairs(v1,v2)and(v2,v1)
representsameedge.
Directedgraph:Inthistheorderofthe
verticesrepresentinganedgeis
important.Thispairisrepresentedas
<v1,v2>wherev1isthetailandv2is
headofv2.Thus<v1,v2>and<v2,v1>
representsdifferentedges.

Undirected graph
•Set of vertices={1,2,3,4,5}
•Set of edges= {(1,2),(1,3),(1,4),(2,3),
(2,4),(2,5),(3,4),(4,5)}
1
5
2
43

Directed Graphs
•Set of vertices={1,2,3}
•Set of edges= {<1,2>, <2,1>, <2,3>,
<3,2>}
1
2
3

Adjacent Vectors & Incident Edges
•Inanundirectedgraphif(v1,v2)isan
edgeinthesetofedges,thenthe
verticesv1andv2aresaidtobe
adjacentandtheedge(v1,v2)is
incidentonverticesv1andv2.
•If<v1,v2>isadirectededge,then
vertexv1issaidtobeadjacenttov2
whilev2isadjacentfromv1.
•Theedge<v1,v2>isincidenttov1and
v2.

Continue…
•Thevertex2inthe
shownundirected
graphisadjacentto
vertices1,3,4and
5.Theedgesincident
onvertex3are
(1,3),(2,3) and
(3,4).
1
5
2
43

Continue…
•Intheshown
directedgraphthe
edgesincidentto
vertex 2 are
<1,2>,<2,1>,<2,3>
and<3,2>.
1
2
3

Terminology of Graphs
•Weightedgraph:A
graphissaidtobe
weightedgraphif
it’sedgeshavebeen
assignedsomenon-
negativevalueas
weight.Aweighted
graphisknownas
network.
1
2
4
3
2
5
3
6

Continue…
•Degree:Inan
undirectedgraph,
thenumberofedges
connectedtoanode
iscalledthedegree
ofthatnode.Where
isindigraph,there
aretwodegreesfor
everynodetheyare
indegree and
outdegree.
1 2
4
3

Continue…
•Indegree: The
indegreeofnodeis
thenumberofedges
comingtothat
node.
•Outdegree:Theout
degreeofnodeis
thenumberofedges
goingoutsidethat
node.
1 2
4
3

Continue…
•Isolatednode:Ifanynodehasno
edgesconnectedwithanyothernode
thenitsdegreewillbezero(0)andit
willbecalledasisolatednode.
A
D
C
B
E

Continue…
•Source:Anode,
which has no
incomingedges,but
hasoutgoingedges,
iscalledasource.
1 2
43

Continue…
•Connectedgraph:
Anundirectedgraph
issaidtobe
connectedifthereis
apathfromany
nodeofgraphto
anyothernode.
A
D
C
B
E

Continue…
•Completegraph:An
undirectedgraph
willcontainn(n-1)/2
edgesiscalleda
complete graph
whereasinthecase
ofdigraphwill
contain n(n-1)
edges,wherenis
thetotalnumberof
nodesinthegraph.
1
2
3
1
2
3

Continue…
•Loop:Anedgewill
becalledlooporself
edgeifitstartsand
endsonthesame
node.
1
2
43

Breadth first search
•Thegeneralidea
behindabreadth
first traversal
beginningata
startingnodeAisas
following.Wefirst
examinethestarting
nodeA,andthenits
neighborsafterthat
theneighborsofits
neighbors. •A BDE CFG
A B
D E
C
GF

Depth first search
•InDepthfirstsearch
techniquealsowetake
onenodeasstarting
node.Thengotothe
path which from
startingnodeandvisit
allthenodeswhichare
inthepath.Whenwe
reachatthelastnode
thenwe traverse
anotherpathstarting
fromthatnode
T
K
N
Y
H A
U
O
THANK YOU
Tags