SlideShare a Scribd company logo
1 of 35
GRAPHS
AUTHOR:
PRINCE KUMAR
MSC CS
5
1
2
4 3
CONTENTS…
 INTRODUCTION
 START WITH GRAPHS
 BASIC TERMINOLOGIES OF GRAPHS
 VARIANCE OF GRAPHS
 MINIMAL SPANNING TREES
 IMPLEMENTATION OF GRAPHS
 TRAVERSAL IN GRAPHS: BFS, DFS
 KRUSKAL’s ALGORITHM
 PRIM’s ALGORITHM
 DIZKASTRA ALGORITHM OF SHORTEST PATH
INTRODUCTION
 This presentation is prepared as a tutorial package on basic graph
theory and algorithms and its implementation as a data structure.
 It also includes some assignments for practice on algorithms.
 Hope this presentation will help the students to understand the
concepts of elementary graph theory.
 PRINCE KUMAR
START WITH GRAPHS
 A Graph G(V,E) is defined as tuple where_
 V ≠ Φ is a finite set of vertices.
 E is set of edges between given vertices.
 Ex. G(V,E) is a graph as below_
V = {1,2,3,4,5}
E = {
{1,2},{2,3},{3,5},{5,4},{1,4},{2,5}
}
4
5
1
3
2
BASIC TERMINOLOGIES OF GRAPHS
 Adjacent
 In a graph G(V,E) if u and v V such that { u , v } E then u and v are
called adjacent to each other.
 Example: in given figure:
2, 3, 4 are adjacent to 5.
4
5
1
3
2
BASIC TERMINOLOGIES OF GRAPHS
 Degree of a vertex: deg(v)
 No. of adjacent to a given vertex is its degree.
 Example: in given figure:
vertices 2, 3 and 4 are adjacent to vertex 5
so it has degree 3 i.e. deg(5) = 3 .
4
5
1
3
2
BASIC TERMINOLOGIES OF GRAPHS
 On the basis of degree, vertices are called_
 Isolated : 0 degree , e.g. Vertex 6,
 Pendent : 1 degree, e.g. Vertex 3,
 Odd : odd degree, e.g. Vertex 5,
 Even : even degree, e.g. Vertex 1.
4
5
1
3
2
6
BASIC TERMINOLOGIES OF GRAPHS
 In a graph ,
The sum of degrees of all the vertices is twice of no. of
edges.
𝑖=1
𝑛
deg 𝑉𝑖 = 2|𝐸|
VARIANCE OF GRAPHS
 COMPLETE GRAPH
 A graph G(V,E) is called as complete graph and denoted as K|V| if
each vertices have degree |V|-1.
 Fig. K5
5
1
2
4 3
VARIANCE OF GRAPHS
 N-REGULAR GRAPH
 A graph G(V,E) is called as N- regular graph if each vertices have
degree N.
 Fig: 4 Regular Graph
5
1
2
4 3
VARIANCE OF GRAPHS
 CONNECTED GRAPH
 A graph G(V,E) is called connected if in between any two vertices there is a
path else it is disconnected.
Fig: (a) Connected (b) Disconnected
5
1
2
4 3
5
1
2
4 3
6
6
VARIANCE OF GRAPHS
 WEIGHTED GRAPH
 A graph G(V,E) is called weighted if all the edges in the graph
having some cost.
5
1
2
4 3
6
a
b
c
d
ef
g
h
VARIANCE OF GRAPHS
 SUBGRAPH
 A graph S(V’,E’) is called subgraph of graph G(V,E) if V’ is subset of
V and E’ is subset of E.
5
1
2
4 3
6
5
1
2
6
VARIANCE OF GRAPHS
 SPANNING SUBGRAPH
 A graph S(V,E’) is called spanning subgraph of graph G(V,E) if E’ is
subset of E.
5
1
2
4 3
6
5
1
2
4 3
6
VARIANCE OF GRAPHS
 TREE
 A tree is a connected graph with no cycles.
5
1
2
4 3
6
MINIMAL SPANNING TREES
 A spanning subgraph of a given graph is called its minimal
spanning tree if_
 It is a tree.
 The total weight on the all the edges in tree is minimal.
5
1
2
4 3
6
2
3
4
1
25
1
3
5
1
2
4 3
6
2
3
1
2
1
IMPLEMENTATION OF GRAPHS
 A graph is represented in memory by_
 Adjacency Matrix : Through 2D arrays.
 The adjacency matrix of a graph G(V, E) is defined as _
A(G) = { a i,j }
where a i,j = { 1 or w if (vi, vj) E
0 else
}
IMPLEMENTATION OF GRAPHS
 Adjacency matrix for adjacent graph is as below_
5
1
2
4 3
0 1 0 0 1
1 0 1 1 1
0 1 0 1 0
0 1 1 0 1
1 1 0 1 0
IMPLEMENTATION OF GRAPHS
 Adjacency List : Through Linked Lists.
5
1
2
4 3
1
2
3
4
5
2 5
1 3 4 5
2 4
2 3 5
1 2 4
TRAVERSALS IN GRAPHS
 For searching an element in the graph generally
we use two types of traversals as below_
 Breadth First Search (BFS):
It searches a node in adjacent of a given node and so on.
It is used when it is known that the target is near to the
source.
TRAVERSALS IN GRAPHS
 Breadth First Search (BFS) Algorithm:
 Let the algorithm assumes A graph G(V,E) is connected is
given with source say root( r ).
 It maintains a queue to keep track of the nodes which are
traversed or which are not.
 Traversal status or say color of each node shows whether
 It is unseen : WHITE , source is yellow.
 It is seen only : YELLOW
 It is processed : BLACK
TRAVERSALS IN GRAPHS
 Put the root ‘r’ into the queue;
 While(queue ≠ Φ)
 u = dequeuer(queue)
 for all v in adj_list of u
 if color of v is white then
 Color v by Yelow.
 Enqueue v in queue.
 Color u by black
TRAVERSALS IN GRAPHS
5
1
2
4 3
1
2
3
4
5
2 5
1 3 4 5
2 4
2 3 5
1 2 4
 Lets travers using BFS algorithm in following graph Let source is 1.
QUEUE12 5 3 4
TRAVERSALS IN GRAPHS
 Depth First Search (DFS):
It searches a node in the path starting of a given node
and until the end of the path, if the target is not in that
path keep backtrack on that path and select a new path
from predecessor nodes and so on .
It is used when it is known that the target is in the depth
of the path starting from the source.
TRAVERSALS IN GRAPHS
 Depth First Search (BFS) Algorithm:
 Let the algorithm assumes A graph G(V,E) is connected is
given with source say root( r ).
 It maintains a stack to keep track of the nodes which are
traversed or which are not .
 Traversal status or say color of each node shows whether
 It is unseen : WHITE , source is yellow.
 It is seen only : YELLOW
 It is processed : BLACK
TRAVERSALS IN GRAPHS
 DFS(G)
 for each u G.V
 Color u white
 Set u.Π as Null
 for each u G.V
 If color of u is white
 VISIT(u)
 VISIT(u)
 Color u yellow
 for each v adj_list of u
 If color of v is white
 Set v. Π as Null
 VISIT(v)
 Color u black
ASSIGNMENT
 Q. Traverse the following given graph using DFS algorithm assuming
‘F’ as a source vertex.
A
B
G
F D
E
C
6
4
3
4
7
5
3
2
1
KRUSKAL’S ALGORITHM
 This algorithm gives MST for a given graph G(V,E).
 Steps
 Sort all the edges in ascending order by weight and keep then in a Queue.
 Make a spanning subgraph say S(V,Φ) of the given graph with no edges.
 Dequeue an edge from the queue and add in the S if it’s not making cycle
in the resultant graph.
 Follow above step until the queue will be empty.
KRUSKAL’S ALGORITHM
5
1
2
4 3
6
2
3
4
1
25
1
3
5
1
2
4 3
6
2
3
1
2
1
2-6 4-3 2-3 1-5 1-2 5-4 5-2 4-2
1 1 2 2 3 3 4 5
PRIM’S ALGORITHM
 This algorithm also grows a MST of a given graph G(V,E) starting with
a source say root (r).
 It adds an edge of minimal weight and doesn’t making cycle in the
resultant graph to an isolated vertex.
 It uses a min heap(Q) structure to grow vertex with minimal distance
of connecting edge.
 A weight function(w) that gives the weight of the edge between two
vertices if exists.
 Initial configuration
 It assumes that the distance(d) of all other vertices are infinite from the
given source and their predecessor(Π) is NULL.
PRIM’S ALGORITHM
Prims_algo(G,w,r)
1. r.d <= 0
2. Q <= G.V
3. while Q is not empty
1. u <= Extract_min(Q)
2. for each v Adj_List[u]
1. If v Q and w(u,v) < v.d
1. v.Π <= u
2. v.d <= w(u,v)
ASSIGNMENT
 Q. Grow MST for the following graph using Prim’s algorithm assuming
the source is ‘C’.
A
B
G
F D
E
C
6
4
3
4
7
5
3
2
1
DIZKASTRA’S ALGORITHM FOR
SHORTEST PATH
 It gives shortest path to all the
target vertices in a given
weighted, connected graph
G(V,E) with respect to a given
source vertex say ‘S’.
 It maintains a min heap let Q
to maintain the traversal in the
graph.
 DIJKASTRA_ALGO(G,S)
 Initialize the cost of all the target vertices ∞.
 Set the predecessor as Null.
 Set cost of S 0.
 Get a set R = Φ
 PUT(Q, G.V)
 WHILE Q is not Null
 u = Extract(Q)
 R = R U u
 For each vertex in adj_list of u
 Maintain the lesser cost of paths for the
adjacents of u.
ASSIGNMENT
 Q.Find out shortest distances for all the vertices in the following graph
assuming the source is ‘C’.
A
B
G
F D
E
C
6
4
3
4
7
5
3
2
1
THANKYOU
MAIL ME: KRPRINCE8888@GMAIL.COM

More Related Content

What's hot

Skiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivitySkiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivityzukun
 
Analysis and design of algorithms part 4
Analysis and design of algorithms part 4Analysis and design of algorithms part 4
Analysis and design of algorithms part 4Deepak John
 
BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmMSA Technosoft
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)Madhu Bala
 
Algorithms of graph
Algorithms of graphAlgorithms of graph
Algorithms of graphgetacew
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Shuvongkor Barman
 
Data Algorithms And Analysis
Data Algorithms And AnalysisData Algorithms And Analysis
Data Algorithms And Analysisgarishma bhatia
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph AJAL A J
 
Lecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star searchLecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star searchHema Kashyap
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs applicationUmme habiba
 
Prim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmPrim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmAcad
 

What's hot (20)

Skiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivitySkiena algorithm 2007 lecture12 topological sort connectivity
Skiena algorithm 2007 lecture12 topological sort connectivity
 
BFS
BFSBFS
BFS
 
18 Basic Graph Algorithms
18 Basic Graph Algorithms18 Basic Graph Algorithms
18 Basic Graph Algorithms
 
Analysis and design of algorithms part 4
Analysis and design of algorithms part 4Analysis and design of algorithms part 4
Analysis and design of algorithms part 4
 
BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal Algorithm
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Lecture13
Lecture13Lecture13
Lecture13
 
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
GRAPH APPLICATION - MINIMUM SPANNING TREE (MST)
 
Algorithms of graph
Algorithms of graphAlgorithms of graph
Algorithms of graph
 
Data structure
Data structureData structure
Data structure
 
Graph
GraphGraph
Graph
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Data Algorithms And Analysis
Data Algorithms And AnalysisData Algorithms And Analysis
Data Algorithms And Analysis
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
 
1535 graph algorithms
1535 graph algorithms1535 graph algorithms
1535 graph algorithms
 
Lecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star searchLecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star search
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
 
Prim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmPrim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithm
 
Slides
SlidesSlides
Slides
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 

Similar to Graphs

Algorithm to count number of disjoint paths
Algorithm to count number of disjoint pathsAlgorithm to count number of disjoint paths
Algorithm to count number of disjoint pathsSujith Jay Nair
 
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHMGRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHMhimanshumishra19dec
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabatinabati
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithmmeisamstar
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx pptDhruvilSTATUS
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxasimshahzad8611
 
ON ALGORITHMIC PROBLEMS CONCERNING GRAPHS OF HIGHER DEGREE OF SYMMETRY
ON ALGORITHMIC PROBLEMS CONCERNING GRAPHS OF HIGHER DEGREE OF SYMMETRYON ALGORITHMIC PROBLEMS CONCERNING GRAPHS OF HIGHER DEGREE OF SYMMETRY
ON ALGORITHMIC PROBLEMS CONCERNING GRAPHS OF HIGHER DEGREE OF SYMMETRYFransiskeran
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxwhittemorelucilla
 

Similar to Graphs (20)

Graphs
GraphsGraphs
Graphs
 
FCS (graphs).pptx
FCS (graphs).pptxFCS (graphs).pptx
FCS (graphs).pptx
 
Algorithm to count number of disjoint paths
Algorithm to count number of disjoint pathsAlgorithm to count number of disjoint paths
Algorithm to count number of disjoint paths
 
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHMGRAPH - DISCRETE STRUCTURE AND ALGORITHM
GRAPH - DISCRETE STRUCTURE AND ALGORITHM
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabati
 
Unit26 shortest pathalgorithm
Unit26 shortest pathalgorithmUnit26 shortest pathalgorithm
Unit26 shortest pathalgorithm
 
Graphs
GraphsGraphs
Graphs
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Graphs
GraphsGraphs
Graphs
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
 
Graph.pptx
Graph.pptxGraph.pptx
Graph.pptx
 
logic.pptx
logic.pptxlogic.pptx
logic.pptx
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
 
ON ALGORITHMIC PROBLEMS CONCERNING GRAPHS OF HIGHER DEGREE OF SYMMETRY
ON ALGORITHMIC PROBLEMS CONCERNING GRAPHS OF HIGHER DEGREE OF SYMMETRYON ALGORITHMIC PROBLEMS CONCERNING GRAPHS OF HIGHER DEGREE OF SYMMETRY
ON ALGORITHMIC PROBLEMS CONCERNING GRAPHS OF HIGHER DEGREE OF SYMMETRY
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
 
DIGITAL TEXT BOOK
DIGITAL TEXT BOOKDIGITAL TEXT BOOK
DIGITAL TEXT BOOK
 
Aj26225229
Aj26225229Aj26225229
Aj26225229
 

More from PRINCE KUMAR

More from PRINCE KUMAR (11)

RIP vs OSPF
RIP vs OSPFRIP vs OSPF
RIP vs OSPF
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++
 
Fddi
FddiFddi
Fddi
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
basics of php
 basics of php basics of php
basics of php
 
php
phpphp
php
 
Connectivity devices
Connectivity devicesConnectivity devices
Connectivity devices
 
Tcp ip tutorial
Tcp ip tutorialTcp ip tutorial
Tcp ip tutorial
 
OSI layers
OSI layersOSI layers
OSI layers
 
NETWORKS & TOPOLOGY
NETWORKS & TOPOLOGYNETWORKS & TOPOLOGY
NETWORKS & TOPOLOGY
 
BUILDING WEBSITES ON WORDPRESS
BUILDING WEBSITES ON WORDPRESSBUILDING WEBSITES ON WORDPRESS
BUILDING WEBSITES ON WORDPRESS
 

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
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
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
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
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
 

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
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
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🔝
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
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
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
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
 

Graphs

  • 2. CONTENTS…  INTRODUCTION  START WITH GRAPHS  BASIC TERMINOLOGIES OF GRAPHS  VARIANCE OF GRAPHS  MINIMAL SPANNING TREES  IMPLEMENTATION OF GRAPHS  TRAVERSAL IN GRAPHS: BFS, DFS  KRUSKAL’s ALGORITHM  PRIM’s ALGORITHM  DIZKASTRA ALGORITHM OF SHORTEST PATH
  • 3. INTRODUCTION  This presentation is prepared as a tutorial package on basic graph theory and algorithms and its implementation as a data structure.  It also includes some assignments for practice on algorithms.  Hope this presentation will help the students to understand the concepts of elementary graph theory.  PRINCE KUMAR
  • 4. START WITH GRAPHS  A Graph G(V,E) is defined as tuple where_  V ≠ Φ is a finite set of vertices.  E is set of edges between given vertices.  Ex. G(V,E) is a graph as below_ V = {1,2,3,4,5} E = { {1,2},{2,3},{3,5},{5,4},{1,4},{2,5} } 4 5 1 3 2
  • 5. BASIC TERMINOLOGIES OF GRAPHS  Adjacent  In a graph G(V,E) if u and v V such that { u , v } E then u and v are called adjacent to each other.  Example: in given figure: 2, 3, 4 are adjacent to 5. 4 5 1 3 2
  • 6. BASIC TERMINOLOGIES OF GRAPHS  Degree of a vertex: deg(v)  No. of adjacent to a given vertex is its degree.  Example: in given figure: vertices 2, 3 and 4 are adjacent to vertex 5 so it has degree 3 i.e. deg(5) = 3 . 4 5 1 3 2
  • 7. BASIC TERMINOLOGIES OF GRAPHS  On the basis of degree, vertices are called_  Isolated : 0 degree , e.g. Vertex 6,  Pendent : 1 degree, e.g. Vertex 3,  Odd : odd degree, e.g. Vertex 5,  Even : even degree, e.g. Vertex 1. 4 5 1 3 2 6
  • 8. BASIC TERMINOLOGIES OF GRAPHS  In a graph , The sum of degrees of all the vertices is twice of no. of edges. 𝑖=1 𝑛 deg 𝑉𝑖 = 2|𝐸|
  • 9. VARIANCE OF GRAPHS  COMPLETE GRAPH  A graph G(V,E) is called as complete graph and denoted as K|V| if each vertices have degree |V|-1.  Fig. K5 5 1 2 4 3
  • 10. VARIANCE OF GRAPHS  N-REGULAR GRAPH  A graph G(V,E) is called as N- regular graph if each vertices have degree N.  Fig: 4 Regular Graph 5 1 2 4 3
  • 11. VARIANCE OF GRAPHS  CONNECTED GRAPH  A graph G(V,E) is called connected if in between any two vertices there is a path else it is disconnected. Fig: (a) Connected (b) Disconnected 5 1 2 4 3 5 1 2 4 3 6 6
  • 12. VARIANCE OF GRAPHS  WEIGHTED GRAPH  A graph G(V,E) is called weighted if all the edges in the graph having some cost. 5 1 2 4 3 6 a b c d ef g h
  • 13. VARIANCE OF GRAPHS  SUBGRAPH  A graph S(V’,E’) is called subgraph of graph G(V,E) if V’ is subset of V and E’ is subset of E. 5 1 2 4 3 6 5 1 2 6
  • 14. VARIANCE OF GRAPHS  SPANNING SUBGRAPH  A graph S(V,E’) is called spanning subgraph of graph G(V,E) if E’ is subset of E. 5 1 2 4 3 6 5 1 2 4 3 6
  • 15. VARIANCE OF GRAPHS  TREE  A tree is a connected graph with no cycles. 5 1 2 4 3 6
  • 16. MINIMAL SPANNING TREES  A spanning subgraph of a given graph is called its minimal spanning tree if_  It is a tree.  The total weight on the all the edges in tree is minimal. 5 1 2 4 3 6 2 3 4 1 25 1 3 5 1 2 4 3 6 2 3 1 2 1
  • 17. IMPLEMENTATION OF GRAPHS  A graph is represented in memory by_  Adjacency Matrix : Through 2D arrays.  The adjacency matrix of a graph G(V, E) is defined as _ A(G) = { a i,j } where a i,j = { 1 or w if (vi, vj) E 0 else }
  • 18. IMPLEMENTATION OF GRAPHS  Adjacency matrix for adjacent graph is as below_ 5 1 2 4 3 0 1 0 0 1 1 0 1 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0
  • 19. IMPLEMENTATION OF GRAPHS  Adjacency List : Through Linked Lists. 5 1 2 4 3 1 2 3 4 5 2 5 1 3 4 5 2 4 2 3 5 1 2 4
  • 20. TRAVERSALS IN GRAPHS  For searching an element in the graph generally we use two types of traversals as below_  Breadth First Search (BFS): It searches a node in adjacent of a given node and so on. It is used when it is known that the target is near to the source.
  • 21. TRAVERSALS IN GRAPHS  Breadth First Search (BFS) Algorithm:  Let the algorithm assumes A graph G(V,E) is connected is given with source say root( r ).  It maintains a queue to keep track of the nodes which are traversed or which are not.  Traversal status or say color of each node shows whether  It is unseen : WHITE , source is yellow.  It is seen only : YELLOW  It is processed : BLACK
  • 22. TRAVERSALS IN GRAPHS  Put the root ‘r’ into the queue;  While(queue ≠ Φ)  u = dequeuer(queue)  for all v in adj_list of u  if color of v is white then  Color v by Yelow.  Enqueue v in queue.  Color u by black
  • 23. TRAVERSALS IN GRAPHS 5 1 2 4 3 1 2 3 4 5 2 5 1 3 4 5 2 4 2 3 5 1 2 4  Lets travers using BFS algorithm in following graph Let source is 1. QUEUE12 5 3 4
  • 24. TRAVERSALS IN GRAPHS  Depth First Search (DFS): It searches a node in the path starting of a given node and until the end of the path, if the target is not in that path keep backtrack on that path and select a new path from predecessor nodes and so on . It is used when it is known that the target is in the depth of the path starting from the source.
  • 25. TRAVERSALS IN GRAPHS  Depth First Search (BFS) Algorithm:  Let the algorithm assumes A graph G(V,E) is connected is given with source say root( r ).  It maintains a stack to keep track of the nodes which are traversed or which are not .  Traversal status or say color of each node shows whether  It is unseen : WHITE , source is yellow.  It is seen only : YELLOW  It is processed : BLACK
  • 26. TRAVERSALS IN GRAPHS  DFS(G)  for each u G.V  Color u white  Set u.Π as Null  for each u G.V  If color of u is white  VISIT(u)  VISIT(u)  Color u yellow  for each v adj_list of u  If color of v is white  Set v. Π as Null  VISIT(v)  Color u black
  • 27. ASSIGNMENT  Q. Traverse the following given graph using DFS algorithm assuming ‘F’ as a source vertex. A B G F D E C 6 4 3 4 7 5 3 2 1
  • 28. KRUSKAL’S ALGORITHM  This algorithm gives MST for a given graph G(V,E).  Steps  Sort all the edges in ascending order by weight and keep then in a Queue.  Make a spanning subgraph say S(V,Φ) of the given graph with no edges.  Dequeue an edge from the queue and add in the S if it’s not making cycle in the resultant graph.  Follow above step until the queue will be empty.
  • 29. KRUSKAL’S ALGORITHM 5 1 2 4 3 6 2 3 4 1 25 1 3 5 1 2 4 3 6 2 3 1 2 1 2-6 4-3 2-3 1-5 1-2 5-4 5-2 4-2 1 1 2 2 3 3 4 5
  • 30. PRIM’S ALGORITHM  This algorithm also grows a MST of a given graph G(V,E) starting with a source say root (r).  It adds an edge of minimal weight and doesn’t making cycle in the resultant graph to an isolated vertex.  It uses a min heap(Q) structure to grow vertex with minimal distance of connecting edge.  A weight function(w) that gives the weight of the edge between two vertices if exists.  Initial configuration  It assumes that the distance(d) of all other vertices are infinite from the given source and their predecessor(Π) is NULL.
  • 31. PRIM’S ALGORITHM Prims_algo(G,w,r) 1. r.d <= 0 2. Q <= G.V 3. while Q is not empty 1. u <= Extract_min(Q) 2. for each v Adj_List[u] 1. If v Q and w(u,v) < v.d 1. v.Π <= u 2. v.d <= w(u,v)
  • 32. ASSIGNMENT  Q. Grow MST for the following graph using Prim’s algorithm assuming the source is ‘C’. A B G F D E C 6 4 3 4 7 5 3 2 1
  • 33. DIZKASTRA’S ALGORITHM FOR SHORTEST PATH  It gives shortest path to all the target vertices in a given weighted, connected graph G(V,E) with respect to a given source vertex say ‘S’.  It maintains a min heap let Q to maintain the traversal in the graph.  DIJKASTRA_ALGO(G,S)  Initialize the cost of all the target vertices ∞.  Set the predecessor as Null.  Set cost of S 0.  Get a set R = Φ  PUT(Q, G.V)  WHILE Q is not Null  u = Extract(Q)  R = R U u  For each vertex in adj_list of u  Maintain the lesser cost of paths for the adjacents of u.
  • 34. ASSIGNMENT  Q.Find out shortest distances for all the vertices in the following graph assuming the source is ‘C’. A B G F D E C 6 4 3 4 7 5 3 2 1