SlideShare a Scribd company logo
1 of 19
Graph
What is Graph ?
A graph G = { V , E }; a finite set of ordered pairs, called
edges E, and vertices V.
Example of Graph:
Graph Definitions
Directed graph  Undirected graph
7
19
21
1
12
4
3
22
2
3
G
J
F
D
A
E C H
4
Adjacency Matrix:
Adjacency List:
Degree:
Implementation of Graph:
calculating degree:
#include <stdio.h>
int main ()
{
int graph[4][4],i,j,in=0,out=0;
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
scanf("%d",&graph[i][j]);
}
}
for(j=0; j<4; j++)
{
for(i=0; i<4; i++)
{
if (graph[i][j]==1)
in++;
}
printf("In degree of %d is %dn",j,in);
in=0;
}
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
if (graph[i][j]==1)
out++;
}
printf("Out degree of %d is %dn",i,out);
out=0;
}
return 0;
}
TREE
Introduction of a Tree
B C
FD E G
A
subtree
Tree Terminology
 Root
 Siblings
 Internal node
 External node
 Ancestors
 Descendant
 Depth
 Height
 Degree
 Subtree
A
B DC
G HE F
I J K
Binary Trees
 Each node has at most 2 children
10
17
159
6 5 8
Root node
Left subtree
Right child
Right child
Left child
14
Binary Search Tree:
17
199
6 12 25
15
Tree Traversal
Preorder
Data Left Right
<root><left><right>
F,D,B,A,C,E,J,G,I,H,K
void preorder(node *root){
if(root!=NULL){
printf(“%c”,root->data);
if(root->left!=NULL)
preorder(root->left);
if(root->right!=NULL)
preorder(root->right);
}
}
F
JD
I
E KB G
A C
Root
H
16
Tree Traversal
Inorder
<left><root><right>
A,B,C,D,E,F,G,H,I,J,K
void inorder(node *root){
if(root!=NULL){
if(root->left!=NULL)
inorder(root->left);
printf(“%c”,root->data);
if(root->right!=NULL)
inorder(root->right);
}
}
F
JD
I
E KB G
A C
Root
H
17
Tree Traversal
pOSTORDER
Left Right Data
<left><right><root>
A,C,B,E,D,H,I,G,K,J,F,A,B
void postorder(node *root){
if(root!=NULL){
if(root->left!=NULL)
postorder(root->left);
if(root->right!=NULL)
postorder(root->right);
printf(“%c”,root->data);
}
}
F
JD
I
E KB G
A C
Root
H
18
Thank You

More Related Content

What's hot

Arreglos bidimencionales carla montilla
Arreglos bidimencionales  carla montillaArreglos bidimencionales  carla montilla
Arreglos bidimencionales carla montillaCARLA MONTILLA
 
[程式設計]Blackfriday
[程式設計]Blackfriday[程式設計]Blackfriday
[程式設計]BlackfridayHui-Shih Leng
 
Eso3 tablasestadisticas blog
Eso3 tablasestadisticas blogEso3 tablasestadisticas blog
Eso3 tablasestadisticas blogMarta Martín
 
Let us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solutionLet us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solutionrohit kumar
 
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solutionLet us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solutionHazrat Bilal
 
Let us c chapter 4 solution
Let us c chapter 4 solutionLet us c chapter 4 solution
Let us c chapter 4 solutionrohit kumar
 
ORM is an Offensive Anti-Pattern
ORM is an Offensive Anti-PatternORM is an Offensive Anti-Pattern
ORM is an Offensive Anti-PatternYegor Bugayenko
 
Arithmetic instructions
Arithmetic instructionsArithmetic instructions
Arithmetic instructionsLearn By Watch
 
SPL 11.1 | Problems on Loop , Nested Loop
SPL 11.1 | Problems on Loop , Nested LoopSPL 11.1 | Problems on Loop , Nested Loop
SPL 11.1 | Problems on Loop , Nested LoopMohammad Imam Hossain
 
Lecture 8 increment_and_decrement_operators
Lecture 8 increment_and_decrement_operatorsLecture 8 increment_and_decrement_operators
Lecture 8 increment_and_decrement_operatorseShikshak
 
regular polygon
regular polygonregular polygon
regular polygonMEDHARANIS
 
[Worksheet] sets (extra)
[Worksheet] sets (extra)[Worksheet] sets (extra)
[Worksheet] sets (extra)Ragulan Dev
 

What's hot (19)

Arreglos bidimencionales carla montilla
Arreglos bidimencionales  carla montillaArreglos bidimencionales  carla montilla
Arreglos bidimencionales carla montilla
 
[程式設計]Blackfriday
[程式設計]Blackfriday[程式設計]Blackfriday
[程式設計]Blackfriday
 
Ds
DsDs
Ds
 
C programming
C programmingC programming
C programming
 
Aggregate
AggregateAggregate
Aggregate
 
Array
ArrayArray
Array
 
Eso3 tablasestadisticas blog
Eso3 tablasestadisticas blogEso3 tablasestadisticas blog
Eso3 tablasestadisticas blog
 
Let us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solutionLet us c(by yashwant kanetkar) chapter 2 solution
Let us c(by yashwant kanetkar) chapter 2 solution
 
week-21x
week-21xweek-21x
week-21x
 
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solutionLet us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
 
Let us c chapter 4 solution
Let us c chapter 4 solutionLet us c chapter 4 solution
Let us c chapter 4 solution
 
1D Array
1D Array1D Array
1D Array
 
ORM is an Offensive Anti-Pattern
ORM is an Offensive Anti-PatternORM is an Offensive Anti-Pattern
ORM is an Offensive Anti-Pattern
 
Vcs17
Vcs17Vcs17
Vcs17
 
Arithmetic instructions
Arithmetic instructionsArithmetic instructions
Arithmetic instructions
 
SPL 11.1 | Problems on Loop , Nested Loop
SPL 11.1 | Problems on Loop , Nested LoopSPL 11.1 | Problems on Loop , Nested Loop
SPL 11.1 | Problems on Loop , Nested Loop
 
Lecture 8 increment_and_decrement_operators
Lecture 8 increment_and_decrement_operatorsLecture 8 increment_and_decrement_operators
Lecture 8 increment_and_decrement_operators
 
regular polygon
regular polygonregular polygon
regular polygon
 
[Worksheet] sets (extra)
[Worksheet] sets (extra)[Worksheet] sets (extra)
[Worksheet] sets (extra)
 

Similar to Graph and Tree Fundamentals (20)

Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Dvst
DvstDvst
Dvst
 
Pnno
PnnoPnno
Pnno
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
CSE240 Pointers
CSE240 PointersCSE240 Pointers
CSE240 Pointers
 
Ansi c
Ansi cAnsi c
Ansi c
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 
Array Programs.pdf
Array Programs.pdfArray Programs.pdf
Array Programs.pdf
 
C programs
C programsC programs
C programs
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
 
C
CC
C
 
Bti1022 lab sheet 8
Bti1022 lab sheet 8Bti1022 lab sheet 8
Bti1022 lab sheet 8
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
 
20DCE096_dlp_prac4.pdf
20DCE096_dlp_prac4.pdf20DCE096_dlp_prac4.pdf
20DCE096_dlp_prac4.pdf
 

More from Inamul Hossain Imran (20)

Secondary storage.pptx
Secondary storage.pptxSecondary storage.pptx
Secondary storage.pptx
 
Region based segmentation
Region based segmentationRegion based segmentation
Region based segmentation
 
Thin film electroluminescent display
Thin film electroluminescent displayThin film electroluminescent display
Thin film electroluminescent display
 
CRT (Cathode ray tube)
CRT (Cathode ray tube)CRT (Cathode ray tube)
CRT (Cathode ray tube)
 
Color model
Color modelColor model
Color model
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
DDA (digital differential analyzer)
DDA (digital differential analyzer)DDA (digital differential analyzer)
DDA (digital differential analyzer)
 
Led (light emitting diode )
Led (light emitting diode )Led (light emitting diode )
Led (light emitting diode )
 
Virtual Blood Bank
Virtual Blood BankVirtual Blood Bank
Virtual Blood Bank
 
Compiler vs interpreter
Compiler vs interpreterCompiler vs interpreter
Compiler vs interpreter
 
Monopoly and monopolistic
Monopoly and monopolisticMonopoly and monopolistic
Monopoly and monopolistic
 
Monopolistic market
Monopolistic marketMonopolistic market
Monopolistic market
 
Oligopoly market
Oligopoly  marketOligopoly  market
Oligopoly market
 
Microeconomics vs macroeconomics
Microeconomics vs macroeconomicsMicroeconomics vs macroeconomics
Microeconomics vs macroeconomics
 
Cost and cost curve
Cost and cost curveCost and cost curve
Cost and cost curve
 
Historical Significance of Ahsan Manzil
Historical Significance of  Ahsan ManzilHistorical Significance of  Ahsan Manzil
Historical Significance of Ahsan Manzil
 
Measures of dispersions
Measures of dispersionsMeasures of dispersions
Measures of dispersions
 
Sequential circuits
Sequential circuitsSequential circuits
Sequential circuits
 
Agriculture in bangladesh
Agriculture in bangladeshAgriculture in bangladesh
Agriculture in bangladesh
 
Properties of relations
Properties of relationsProperties of relations
Properties of relations
 

Recently uploaded

Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

Graph and Tree Fundamentals