SlideShare une entreprise Scribd logo
1  sur  30
Data Structure and
Algorithm (CS-102)
Ashok K Turuk
Graph
• A graph is a collection of nodes (or
vertices, singular is vertex) and edges (or
arcs)
– Each node contains an element
– Each edge connects two nodes together (or
possibly the same node to itself) and may
contain an edge attribute

• A directed graph is one in which the edges
have a direction
• An undirected graph is one in which the
edges do not have a direction
2
What is a Graph?
• A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
• An edge e = (u,v) is a pair of vertices
• Example:

a

b

E= {(a,b),(a,c),(a,d),
(b,e),(c,d),(c,e), (d,e)}

c
d

V= {a,b,c,d,e}

e
some problems that can be
represented by a graph

computer networks
airline flights
road map
course prerequisite structure
tasks for completing a job
flow of control through a
program
• many more
•
•
•
•
•
•
a digraph
A
E

B

C

D

V = [A, B, C, D, E]
E = [<A,B>, <B,C>, <C,B>, <A,C>, <A,E>, <C,D>]
Graph terminology
• The size of a graph is the number of nodes in it
• The empty graph has size zero (no nodes)
• If two nodes are connected by an edge, they are
neighbors (and the nodes are adjacent to each other)
• The degree of a node is the number of edges it has
• For directed graphs,
– If a directed edge goes from node S to node D, we call S the
source and D the destination of the edge
• The edge is an out-edge of S and an in-edge of D
• S is a predecessor of D, and D is a successor of S

– The in-degree of a node is the number of in-edges it has
– The out-degree of a node is the number of out-edges it has

6
Terminology:
Path
• path: sequence of
vertices v1,v2,. . .vk
such that consecutive
vertices vi and vi+1 are
adjacent.

3

2
3

3

3

a

b

a

c

b
c

e d

d
abedc

e
bedc
7
More Terminology
• simple path: no repeated vertices
a

b
bec

c
e

d

• cycle: simple path, except that the last vertex is the same
as the first vertex
a

b
acda

c
d

e
Even More Terminology
•connected graph: any two vertices are connected
by some path

connected

not connected

• subgraph: subset of vertices and edges forming a graph
• connected component: maximal connected subgraph. E.g.,
the graph below has 3 connected components.
Subgraphs Examples
0
1

0

0

2
3
G1
0

1

(i)

3

0
1

2
3

0

0

1

1

2

(i)

0
1

1

G3

2

2

(ii)
(iii)
(iv)
(a) Some of the subgraph of G1
0

2

1

2

(ii)

(iii)
(b) Some of the subgraph of G3

(iv)
More…
• tree - connected graph without cycles
• forest - collection of trees
tree

tree

forest
tree
tree
Connectivity
• Let n = #vertices, and m = #edges
• A complete graph: one in which all pairs of vertices
are adjacent
• How many total edges in a complete graph?
– Each of the n vertices is incident to n-1 edges, however, we
would have counted each edge twice! Therefore,
intuitively, m = n(n -1)/2.

• Therefore, if a graph is not complete, m < n(n -1)/2

n 5
m (5
More Connectivity
n = #vertices
m = #edges
• For a tree m = n - 1

n 5
m 4

If m < n - 1, G is not
connected

n 5
m 3
Oriented (Directed) Graph
• A graph where edges are directed
Directed vs. Undirected Graph
• An undirected graph is one in which the
pair of vertices in a edge is unordered,
(v0, v1) = (v1,v0)
• A directed graph is one in which each
edge is a directed pair of vertices, <v0,
tail
head
v1> != <v1,v0>
Graph terminology
• An undirected graph is connected if there is a
path from every node to every other node
• A directed graph is strongly connected if there is
a path from every node to every other node
• A directed graph is weakly connected if the
underlying undirected graph is connected
• Node X is reachable from node Y if there is a path
from Y to X
• A subset of the nodes of the graph is a connected
component (or just a component) if there is a path
from every node in the subset to every other node
in the subset
16
graph data structures
• storing the vertices

– each vertex has a unique identifier and,
maybe, other information
– for efficiency, associate each vertex with a
number that can be used as an index

• storing the edges

– adjacency matrix – represent all possible
edges
– adjacency lists – represent only the
existing edges
storing the vertices
• when a vertex is added to the graph,
assign it a number

– vertices are numbered between 0 and n-1

• graph operations start by looking up
the number associated with a vertex
• many data structures to use
– any of the associative data structures
– for small graphs a vector can be used
• search will be O(n)
the vertex vector
0

A
4

1

E

B

C
2

D
3

0
1
2
3
4

A
B
C
D
E
adjacency matrix
0

A
4

1

E

B

0
1
2
3
4

A
B
C
D
E
0 1 2 3 4

C
2

D
3

0
1
2
3
4

0
0
0
0
0

1
0
1
0
0

1
1
0
0
0

0
0
1
0
0

1
0
0
0
0
0
1

Examples for Adjacency Matrix
0
2

1

3

2

G1

G2

0

1

1

1

0

1

0

1
1

0
1

1
0

1
1

1

0

1

0

0

0

1

1

1

0

symmetric

Asymmetric
maximum # edges?

a V2 matrix is needed for
a graph with V vertices
many graphs are “sparse”
• degree of “sparseness” key factor in
choosing a data structure for edges

– adjacency matrix requires space for all
possible edges
– adjacency list requires space for existing
edges only

• affects amount of memory space
needed
• affects efficiency of graph operations
adjacency lists
0

0
1
2
3
4

A
4

1

E

B

C
2

D
3

0
1
2
3
4

1
2
1

A
B
C
D
E

2
3

4
0
1

2
3

0
1
2
3

1
0
0
0

2
2
1
1

3
3
3
2

0

G1
0
1
2

1
0

1

2
G3

2
Least-Cost Algorithms
Dijkstra’s Algorithm

Find the least cost path from a given node to all
other nodes in the network
N = Set of nodes in the network
s = Source node
M = Set of nodes so far incorporated by the
algorithm
dij = link cost from node i to node j, dii = 0
dij = ∞ if two nodes are not directly connected
dij >= 0 of two nodes are directly connected
Dn = cost of the least-cost path from node s to
node n that is currently known to the algorithm
1. Initialize
M = {s} [ set of nodes so far
incorporated consists of only the
source node]
Dn = dsn for n != s (initial path costs to
the neighbor nodes are simply the link
cost)
2. Find the neighbor node not in M that
has the least-cost path from node s
and incorporate that node into M.
Find w !Є M such that Dw = min { Dj } for j
!Є M
3. Update the least-cost path:
Dn = min[ Dn , Dw + dwn ] for all n !Є M

If the latter term is minimum, path from
s to n is now the path from s to w,
concatenated with the link from w to n
8
5
2
1

2

3
1 2

7

4

6
3

1

8

3

1 5
2

3
5

6

4
It = Iteration
It

M

D2 Path D3 Path D4 Path D5 Path D6 Path

1 {1}

2

1 -2

5

2 {1, 4}

2

1-2

3 {1,2,4}

2

1-2

1-3

1

1-4

∞

4 1-4-3

1

1-4

4 1-4-3

1

1-4

-

∞

-

2 1-4-5

∞

-

2 1-4-5

∞

-

Contenu connexe

Tendances

Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structurehafsa komal
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data StructureKeno benti
 
Skiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strcturesSkiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strctureszukun
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structurePooja Bhojwani
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data StructureProf Ansari
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsYi-Lung Tsai
 
Graph (Data structure)
Graph (Data structure) Graph (Data structure)
Graph (Data structure) shamiur rahman
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structurehamza javed
 
Data Structure : Graph and Graph Traversing
Data Structure : Graph and Graph TraversingData Structure : Graph and Graph Traversing
Data Structure : Graph and Graph TraversingVikas Chandwani
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data StructureAnuj Modi
 
Graphs in c language
Graphs in c languageGraphs in c language
Graphs in c languageSARITHA REDDY
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphsKumar
 

Tendances (20)

Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Skiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strcturesSkiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strctures
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data Structure
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Graph (Data structure)
Graph (Data structure) Graph (Data structure)
Graph (Data structure)
 
Data structure
Data structureData structure
Data structure
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structure
 
Graph
GraphGraph
Graph
 
Data Structure : Graph and Graph Traversing
Data Structure : Graph and Graph TraversingData Structure : Graph and Graph Traversing
Data Structure : Graph and Graph Traversing
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 
Representation
RepresentationRepresentation
Representation
 
Lecture25
Lecture25Lecture25
Lecture25
 
Graphs in c language
Graphs in c languageGraphs in c language
Graphs in c language
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Basics of graph
Basics of graphBasics of graph
Basics of graph
 

En vedette

Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsAakash deep Singhal
 
Lecture 12 data structures and algorithms
Lecture 12 data structures and algorithmsLecture 12 data structures and algorithms
Lecture 12 data structures and algorithmsAakash deep Singhal
 
Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsAakash deep Singhal
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsAakash deep Singhal
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsAakash deep Singhal
 
Lecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsLecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsAakash deep Singhal
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsAakash deep Singhal
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsAakash deep Singhal
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsAakash deep Singhal
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsAakash deep Singhal
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sortingFadhil Ismail
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderAshin Guha Majumder
 
Modern Algorithms and Data Structures - 1. Bloom Filters, Merkle Trees
Modern Algorithms and Data Structures - 1. Bloom Filters, Merkle TreesModern Algorithms and Data Structures - 1. Bloom Filters, Merkle Trees
Modern Algorithms and Data Structures - 1. Bloom Filters, Merkle TreesLorenzo Alberton
 

En vedette (20)

Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Lecture 12 data structures and algorithms
Lecture 12 data structures and algorithmsLecture 12 data structures and algorithms
Lecture 12 data structures and algorithms
 
Subsidence in coal mines
Subsidence in coal minesSubsidence in coal mines
Subsidence in coal mines
 
Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithms
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
 
12111 data structure
12111 data structure12111 data structure
12111 data structure
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Lecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsLecture 16 data structures and algorithms
Lecture 16 data structures and algorithms
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithms
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithms
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithms
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
Sorting
SortingSorting
Sorting
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Modern Algorithms and Data Structures - 1. Bloom Filters, Merkle Trees
Modern Algorithms and Data Structures - 1. Bloom Filters, Merkle TreesModern Algorithms and Data Structures - 1. Bloom Filters, Merkle Trees
Modern Algorithms and Data Structures - 1. Bloom Filters, Merkle Trees
 
Multi ways trees
Multi ways treesMulti ways trees
Multi ways trees
 

Similaire à Lecture 14 data structures and algorithms

Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjtepournima055
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphsssuser034ce1
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphsssuser034ce1
 
Minicourse on Network Science
Minicourse on Network ScienceMinicourse on Network Science
Minicourse on Network SciencePavel Loskot
 
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
 
09_Graphs_handout.pdf
09_Graphs_handout.pdf09_Graphs_handout.pdf
09_Graphs_handout.pdfIsrar63
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptxARVIND SARDAR
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxking779879
 

Similaire à Lecture 14 data structures and algorithms (20)

10.graph
10.graph10.graph
10.graph
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
8150.graphs
8150.graphs8150.graphs
8150.graphs
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Dijkstra
DijkstraDijkstra
Dijkstra
 
d
dd
d
 
ae_722_unstructured_meshes.ppt
ae_722_unstructured_meshes.pptae_722_unstructured_meshes.ppt
ae_722_unstructured_meshes.ppt
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
 
Graph
GraphGraph
Graph
 
Graph therory
Graph theroryGraph therory
Graph therory
 
09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf
 
Minicourse on Network Science
Minicourse on Network ScienceMinicourse on Network Science
Minicourse on Network Science
 
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
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
Graphs
GraphsGraphs
Graphs
 
09_Graphs_handout.pdf
09_Graphs_handout.pdf09_Graphs_handout.pdf
09_Graphs_handout.pdf
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
 

Dernier

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 

Dernier (20)

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
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🔝
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 

Lecture 14 data structures and algorithms

  • 1. Data Structure and Algorithm (CS-102) Ashok K Turuk
  • 2. Graph • A graph is a collection of nodes (or vertices, singular is vertex) and edges (or arcs) – Each node contains an element – Each edge connects two nodes together (or possibly the same node to itself) and may contain an edge attribute • A directed graph is one in which the edges have a direction • An undirected graph is one in which the edges do not have a direction 2
  • 3. What is a Graph? • A graph G = (V,E) is composed of: V: set of vertices E: set of edges connecting the vertices in V • An edge e = (u,v) is a pair of vertices • Example: a b E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)} c d V= {a,b,c,d,e} e
  • 4. some problems that can be represented by a graph computer networks airline flights road map course prerequisite structure tasks for completing a job flow of control through a program • many more • • • • • •
  • 5. a digraph A E B C D V = [A, B, C, D, E] E = [<A,B>, <B,C>, <C,B>, <A,C>, <A,E>, <C,D>]
  • 6. Graph terminology • The size of a graph is the number of nodes in it • The empty graph has size zero (no nodes) • If two nodes are connected by an edge, they are neighbors (and the nodes are adjacent to each other) • The degree of a node is the number of edges it has • For directed graphs, – If a directed edge goes from node S to node D, we call S the source and D the destination of the edge • The edge is an out-edge of S and an in-edge of D • S is a predecessor of D, and D is a successor of S – The in-degree of a node is the number of in-edges it has – The out-degree of a node is the number of out-edges it has 6
  • 7. Terminology: Path • path: sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent. 3 2 3 3 3 a b a c b c e d d abedc e bedc 7
  • 8. More Terminology • simple path: no repeated vertices a b bec c e d • cycle: simple path, except that the last vertex is the same as the first vertex a b acda c d e
  • 9. Even More Terminology •connected graph: any two vertices are connected by some path connected not connected • subgraph: subset of vertices and edges forming a graph • connected component: maximal connected subgraph. E.g., the graph below has 3 connected components.
  • 10. Subgraphs Examples 0 1 0 0 2 3 G1 0 1 (i) 3 0 1 2 3 0 0 1 1 2 (i) 0 1 1 G3 2 2 (ii) (iii) (iv) (a) Some of the subgraph of G1 0 2 1 2 (ii) (iii) (b) Some of the subgraph of G3 (iv)
  • 11. More… • tree - connected graph without cycles • forest - collection of trees tree tree forest tree tree
  • 12. Connectivity • Let n = #vertices, and m = #edges • A complete graph: one in which all pairs of vertices are adjacent • How many total edges in a complete graph? – Each of the n vertices is incident to n-1 edges, however, we would have counted each edge twice! Therefore, intuitively, m = n(n -1)/2. • Therefore, if a graph is not complete, m < n(n -1)/2 n 5 m (5
  • 13. More Connectivity n = #vertices m = #edges • For a tree m = n - 1 n 5 m 4 If m < n - 1, G is not connected n 5 m 3
  • 14. Oriented (Directed) Graph • A graph where edges are directed
  • 15. Directed vs. Undirected Graph • An undirected graph is one in which the pair of vertices in a edge is unordered, (v0, v1) = (v1,v0) • A directed graph is one in which each edge is a directed pair of vertices, <v0, tail head v1> != <v1,v0>
  • 16. Graph terminology • An undirected graph is connected if there is a path from every node to every other node • A directed graph is strongly connected if there is a path from every node to every other node • A directed graph is weakly connected if the underlying undirected graph is connected • Node X is reachable from node Y if there is a path from Y to X • A subset of the nodes of the graph is a connected component (or just a component) if there is a path from every node in the subset to every other node in the subset 16
  • 17. graph data structures • storing the vertices – each vertex has a unique identifier and, maybe, other information – for efficiency, associate each vertex with a number that can be used as an index • storing the edges – adjacency matrix – represent all possible edges – adjacency lists – represent only the existing edges
  • 18. storing the vertices • when a vertex is added to the graph, assign it a number – vertices are numbered between 0 and n-1 • graph operations start by looking up the number associated with a vertex • many data structures to use – any of the associative data structures – for small graphs a vector can be used • search will be O(n)
  • 20. adjacency matrix 0 A 4 1 E B 0 1 2 3 4 A B C D E 0 1 2 3 4 C 2 D 3 0 1 2 3 4 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 0 0 1 0 0 1 0 0 0 0
  • 21. 0 1 Examples for Adjacency Matrix 0 2 1 3 2 G1 G2 0 1 1 1 0 1 0 1 1 0 1 1 0 1 1 1 0 1 0 0 0 1 1 1 0 symmetric Asymmetric
  • 22. maximum # edges? a V2 matrix is needed for a graph with V vertices
  • 23. many graphs are “sparse” • degree of “sparseness” key factor in choosing a data structure for edges – adjacency matrix requires space for all possible edges – adjacency list requires space for existing edges only • affects amount of memory space needed • affects efficiency of graph operations
  • 26. Least-Cost Algorithms Dijkstra’s Algorithm Find the least cost path from a given node to all other nodes in the network N = Set of nodes in the network s = Source node M = Set of nodes so far incorporated by the algorithm dij = link cost from node i to node j, dii = 0 dij = ∞ if two nodes are not directly connected dij >= 0 of two nodes are directly connected Dn = cost of the least-cost path from node s to node n that is currently known to the algorithm
  • 27. 1. Initialize M = {s} [ set of nodes so far incorporated consists of only the source node] Dn = dsn for n != s (initial path costs to the neighbor nodes are simply the link cost) 2. Find the neighbor node not in M that has the least-cost path from node s and incorporate that node into M.
  • 28. Find w !Є M such that Dw = min { Dj } for j !Є M 3. Update the least-cost path: Dn = min[ Dn , Dw + dwn ] for all n !Є M If the latter term is minimum, path from s to n is now the path from s to w, concatenated with the link from w to n
  • 30. It = Iteration It M D2 Path D3 Path D4 Path D5 Path D6 Path 1 {1} 2 1 -2 5 2 {1, 4} 2 1-2 3 {1,2,4} 2 1-2 1-3 1 1-4 ∞ 4 1-4-3 1 1-4 4 1-4-3 1 1-4 - ∞ - 2 1-4-5 ∞ - 2 1-4-5 ∞ -