SlideShare a Scribd company logo
Algorithms
Graphs
Paths and cycles
● A path is a sequence of nodes
v1, v2, …, vN such that (vi,vi+1)E for 0<i<N
■ The length of the path is N-1.
■ Simple path: all vi are distinct, 0<i<N
● A cycle is a path such that v1=vN
■ An acyclic graph has no cycles
Cycles
PIT
BOS
JFK
DTW
LAX
SFO
More useful definitions
● The degree of a vertex is the number of
edges incident to that vertex
● In a directed graph:
● The indegree of a node v is the number of
distinct edges (w,v)E.
● The outdegree of a node v is the number of
distinct edges (v,w)E.
● A node with indegree 0 is a root.
Trees are graphs
● A dag is a directed acyclic graph.
● A tree is a connected acyclic undirected graph.
● A forest is an acyclic undirected graph (not
necessarily connected), i.e., each connected
component is a tree.
Even More Terminology
● subgraph: subset of vertices and edges forming a graph
● connected component: maximal connected subgraph. E.g., the graph below has
3 connected components.
connected not connected
•connected graph: any two vertices are connected by some path
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
n  5
m  3
If m < n - 1, G is not
connected
G = (V, E)
a vertex may have:
0 or more predecessors
0 or more successors
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
Graphs in Computer Science: How
do they help?
● What do they model?
■ An abstraction in Core CS.
○ Examples: VLSI Circuits, Communication Networks, Logical
flow of a computer program, Data structures.
■ An abstraction for data and relationships.
○ Examples: The Web, Social Networks, Flows and Flow
Networks, Biological Data, Taxonomies, Citations, Explicit
relations within a DB system.
● What aspects are studied?
■ Algorithms, Data Structures and Complexity Theory.
■ Characterization and Modeling of Graphs.
■ Implementations of Graph Algorithms in Specific
contexts.
What is a graph?
● A set of vertices and edges
■ Directed/Undirected
■ Weighted/Unweighted
■ Cyclic/Acyclic
vertex
edge
Representation of Graphs
● Adjacency Matrix
■ A V x V array, with matrix[i][j] storing whether
there is an edge between the ith vertex and the jth
vertex
● Adjacency Linked List
■ One linked list per vertex, each storing directly
reachable vertices
Representation of Graphs
Adjacency
Matrix
Adjacency
Linked List
Memory
Storage
O(V2) O(V+E)
Check
whether
(u,v) is an
edge
O(1) O(deg(u))
Find all
adjacent
vertices of a
vertex u
O(V) O(deg(u))
graph variations
● undirected graph (graph)
■ edges do not have a direction
○ (V1, V2) and (V2, V1) are the same edge
● directed graph (digraph)
■ edges have a direction
○ <V1, V2> and <V2, V1> are different edges
● for either type, edges may be weighted or
unweighted
a digraph
A
B
C D
E
V = [A, B, C, D, E]
E = [<A,B>, <B,C>, <C,B>, <A,C>, <A,E>, <C,D>]
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
A
B
C D
E
0
1
2 3
4
0
1
2
3
4
A
B
C
D
E
maximum # edges?
a V2 matrix is needed for
a graph with V vertices
adjacency matrix
A
B
C D
E
0
1
2 3
4
0
1
2
3
4
A
B
C
D
E
0 1 1 0 1
0 0 1 0 0
0 1 0 1 0
0 0 0 0 0
0 0 0 0 0
0
1
2
3
4
0 1 2 3 4
adjacency matrix
A B C D
A 0 1 1 1
B 1 0 0 1
C 1 0 0 1
D 1 1 1 0
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
A
B
C D
E
0
1
2 3
4
0
1
2
3
4
1 2 4
2
1 3
0
1
2
3
4
A
B
C
D
E
Adjacency Lists
A representation of the graph consisting of a list of nodes, with
each node containing a list of its neighboring nodes.
This representation takes O(|V | + |E|) space.
Some graph operations
adjacency matrix adjacency lists
insertEdge
isEdge
#successors?
#predecessors?
O(1)
O(1)
O(V)
O(V)
O(e)
O(e)
O(e)
O(E)
Introduction
● A free tree is a connected undirected graph without a cycle.
■ Note: This definition of tree is different from the one of a rooted tree
● In a free tree |E| = |V| - 1
● Example of a free tree:
● A forest is an acyclic directed or undirected graph consisting of
two or more trees
● The trees in a directed forest are rooted trees
● The trees in an undirected forest are free trees
Graph Traversals
•Both take time: O(V+E)
Use of a stack
● It is very common to use a stack to keep track
of:
■ nodes to be visited next, or
■ nodes that we have already visited.
● Typically, use of a stack leads to a depth-first
visit order.
● Depth-first visit order is “aggressive” in the
sense that it examines complete paths.
Use of a queue
● It is very common to use a queue to keep track
of:
■ nodes to be visited next, or
■ nodes that we have already visited.
● Typically, use of a queue leads to a breadth-
first visit order.
● Breadth-first visit order is “cautious” in the
sense that it examines every path of length i
before going on to paths of length i+1.
Breadth-First Search (BFS)
● BFS tries to search all paths.
● BFS makes use of a queue to store visited (but
not dead) vertices, expanding the path from the
earliest visited vertices.
1
4
3
2
5
6
Simulation of BFS
● Queue: 1 4 3 5 2 6
BFS: Start with Node 5
7
1
5
4
3
2
6
5 1 2 0 4 3 7 6
0
BFS: Start with Node 5
7
1
5
4
3
2
6
5
0
BFS: Node one-away
7
1
5
4
3
2
6
5
0
BFS: Visit 1 and 2
7
1
5
4
3
2
6
5 1 2
0
BFS: Nodes two-away
7
1
5
4
3
2
6
5 1 2
0
BFS: Visit 0 and 4
7
1
5
4
3
2
6
5 1 2 0 4
0
BFS: Nodes three-away
7
1
5
4
3
2
6
5 1 2 0 4
0
BFS: Visit nodes 3 and 7
7
1
5
4
3
2
6
5 1 2 0 4 3 7
0
BFS: Node four-away
7
1
5
4
3
2
6
5 1 2 0 4 3 7
0
BFS: Visit 6
7
1
5
4
3
2
6
5 1 2 0 4 3 7 6
0
Breadth-First Search (BFS)
Breadth-First Search (BFS)
Breadth-First Search (BFS)
Breadth-First Search (BFS)
Breadth-First Search (BFS)
Example
● Breadth-first traversal using a queue.
Order of
Traversal Queue rearA B D E C G F H I
Queue front
A
B D E
C G
F H
I
BFS-tree:
Note: The BFS-tree for undirected graph is a free tree
Analysis of BFS
For a Graph G=(V, E) and n = |V| and m=|E|
• When Adjacency List is used
Complexity is O(m + n)
• When Adjacency Matrix is used
 Scanning each row for checking the connectivity of a Vertex
is in order O(n).
So, Complexity is O(n2)
Depth-First Search (DFS)
● Strategy: Go as far as you can (if you have not
visit there), otherwise, go back and try another
way
Example
0
7
1
5
4
3
2
6
Policy: Visit adjacent nodes in increasing index
order
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7 6 4
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5
Push 5
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5
Pop/Visit/Mark 5
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5
1
2
Push 2, Push 1
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1
2
Pop/Visit/Mark 1
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1
0
2
4
2
Push 4, Push 2,
Push 0
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0
2
4
2
Pop/Visit/Mark 0
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0
3
7
2
4
2
Push 7, Push 3
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3
7
2
4
2
Pop/Visit/Mark 3
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3
2
7
2
4
2
Push 2
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2
7
2
4
2
Pop/Mark/Visit 2
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7
2
4
2
Pop/Mark/Visit 7
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7
6
2
4
2
Push 6
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7 6
2
4
2
Pop/Mark/Visit 6
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7 6
4
2
Pop (don’t visit) 2
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7 6
4
2
Pop/Mark/Visit 4
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7 6
4
Pop (don’t visit) 2
Preorder DFS: Start with Node 5
0
7
1
5
4
3
2
6
5 1 0 3 2 7 6
4
Done
Example
● Depth-first traversal using an explicit stack.
Order of
Traversal StackA B C F E G D H I
The Preorder Depth First Tree:
Note: The DFS-tree for undirected graph is a free tree
Recursive preorder Depth-First Traversal Tracing
At each stage, a set of unvisited adjacent
vertices of the current vertex is generated.
The Preorder Depth First Tree:
Analysis of DFS
For a Graph G=(V, E) and n = |V| and m=|E|
• When Adjacency List is used
Complexity is O(m + n)
• When Adjacency Matrix is used
 Scanning each row for checking the connectivity of a Vertex
is in order O(n).
So, Complexity is O(n2)
DFS uses space O(|V|) in the worst case to store the stack
of vertices on the current search path as well as the set of
already-visited vertices.

More Related Content

What's hot

Graph representation
Graph representationGraph representation
Graph representationTech_MX
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 
Graph data structure
Graph data structureGraph data structure
Graph data structureTech_MX
 
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
Yi-Lung Tsai
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structure
hamza javed
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
Pooja Bhojwani
 
Adjacency list
Adjacency listAdjacency list
Adjacency listStefi Yu
 
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
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
hafsa komal
 
Data Structure : Graph and Graph Traversing
Data Structure : Graph and Graph TraversingData Structure : Graph and Graph Traversing
Data Structure : Graph and Graph Traversing
Vikas Chandwani
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
Madhu Bala
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
Afaq Mansoor Khan
 
Graphss
GraphssGraphss
Graphss
fika sweety
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
Keno benti
 
Graph terminologies & special type graphs
Graph terminologies & special type graphsGraph terminologies & special type graphs
Graph terminologies & special type graphs
Nabeel Ahsen
 
Graph Theory Introduction
Graph Theory IntroductionGraph Theory Introduction
Graph Theory Introduction
MANISH T I
 
Graph (Data structure)
Graph (Data structure) Graph (Data structure)
Graph (Data structure)
shamiur rahman
 

What's hot (20)

Graph representation
Graph representationGraph representation
Graph representation
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
Graph data structure
Graph data structureGraph data structure
Graph data structure
 
Graph theory
Graph theoryGraph theory
Graph theory
 
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
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structure
 
Graph
GraphGraph
Graph
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Adjacency list
Adjacency listAdjacency list
Adjacency list
 
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
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
 
Data Structure : Graph and Graph Traversing
Data Structure : Graph and Graph TraversingData Structure : Graph and Graph Traversing
Data Structure : Graph and Graph Traversing
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graphss
GraphssGraphss
Graphss
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graph terminologies & special type graphs
Graph terminologies & special type graphsGraph terminologies & special type graphs
Graph terminologies & special type graphs
 
Graph Theory Introduction
Graph Theory IntroductionGraph Theory Introduction
Graph Theory Introduction
 
Graph (Data structure)
Graph (Data structure) Graph (Data structure)
Graph (Data structure)
 
Graphs Algorithms
Graphs AlgorithmsGraphs Algorithms
Graphs Algorithms
 

Similar to Graph

Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsAakash deep Singhal
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
Dabbal Singh Mahara
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
Tribhuvan University
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham
 
Graphs
GraphsGraphs
Graphs
GraphsGraphs
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
DhruvilSTATUS
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
pournima055
 
ppt 1.pptx
ppt 1.pptxppt 1.pptx
ppt 1.pptx
ShasidharaniD
 
130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
Ketaki_Pattani
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
Sazzad Hossain
 
Unit VI - Graphs.ppt
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.ppt
HODElex
 
18 Basic Graph Algorithms
18 Basic Graph Algorithms18 Basic Graph Algorithms
18 Basic Graph Algorithms
Andres Mendez-Vazquez
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
bashirabdullah789
 
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
ssuser034ce1
 
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
ssuser034ce1
 

Similar to Graph (20)

10.graph
10.graph10.graph
10.graph
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
 
Graps 2
Graps 2Graps 2
Graps 2
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Graphs
GraphsGraphs
Graphs
 
Graphs
GraphsGraphs
Graphs
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 
ppt 1.pptx
ppt 1.pptxppt 1.pptx
ppt 1.pptx
 
130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Unit VI - Graphs.ppt
Unit VI - Graphs.pptUnit VI - Graphs.ppt
Unit VI - Graphs.ppt
 
18 Basic Graph Algorithms
18 Basic Graph Algorithms18 Basic Graph Algorithms
18 Basic Graph Algorithms
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
 
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
 
8150.graphs
8150.graphs8150.graphs
8150.graphs
 

More from Dr Sandeep Kumar Poonia

Soft computing
Soft computingSoft computing
Soft computing
Dr Sandeep Kumar Poonia
 
An improved memetic search in artificial bee colony algorithm
An improved memetic search in artificial bee colony algorithmAn improved memetic search in artificial bee colony algorithm
An improved memetic search in artificial bee colony algorithm
Dr Sandeep Kumar Poonia
 
Modified position update in spider monkey optimization algorithm
Modified position update in spider monkey optimization algorithmModified position update in spider monkey optimization algorithm
Modified position update in spider monkey optimization algorithm
Dr Sandeep Kumar Poonia
 
Enhanced local search in artificial bee colony algorithm
Enhanced local search in artificial bee colony algorithmEnhanced local search in artificial bee colony algorithm
Enhanced local search in artificial bee colony algorithm
Dr Sandeep Kumar Poonia
 
RMABC
RMABCRMABC
Memetic search in differential evolution algorithm
Memetic search in differential evolution algorithmMemetic search in differential evolution algorithm
Memetic search in differential evolution algorithm
Dr Sandeep Kumar Poonia
 
Improved onlooker bee phase in artificial bee colony algorithm
Improved onlooker bee phase in artificial bee colony algorithmImproved onlooker bee phase in artificial bee colony algorithm
Improved onlooker bee phase in artificial bee colony algorithm
Dr Sandeep Kumar Poonia
 
Comparative study of_hybrids_of_artificial_bee_colony_algorithm
Comparative study of_hybrids_of_artificial_bee_colony_algorithmComparative study of_hybrids_of_artificial_bee_colony_algorithm
Comparative study of_hybrids_of_artificial_bee_colony_algorithm
Dr Sandeep Kumar Poonia
 
A novel hybrid crossover based abc algorithm
A novel hybrid crossover based abc algorithmA novel hybrid crossover based abc algorithm
A novel hybrid crossover based abc algorithm
Dr Sandeep Kumar Poonia
 
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked listsMultiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Dr Sandeep Kumar Poonia
 
Sunzip user tool for data reduction using huffman algorithm
Sunzip user tool for data reduction using huffman algorithmSunzip user tool for data reduction using huffman algorithm
Sunzip user tool for data reduction using huffman algorithm
Dr Sandeep Kumar Poonia
 
New Local Search Strategy in Artificial Bee Colony Algorithm
New Local Search Strategy in Artificial Bee Colony Algorithm New Local Search Strategy in Artificial Bee Colony Algorithm
New Local Search Strategy in Artificial Bee Colony Algorithm
Dr Sandeep Kumar Poonia
 
A new approach of program slicing
A new approach of program slicingA new approach of program slicing
A new approach of program slicing
Dr Sandeep Kumar Poonia
 
Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...Dr Sandeep Kumar Poonia
 
Enhanced abc algo for tsp
Enhanced abc algo for tspEnhanced abc algo for tsp
Enhanced abc algo for tsp
Dr Sandeep Kumar Poonia
 
Performance evaluation of diff routing protocols in wsn using difft network p...
Performance evaluation of diff routing protocols in wsn using difft network p...Performance evaluation of diff routing protocols in wsn using difft network p...
Performance evaluation of diff routing protocols in wsn using difft network p...
Dr Sandeep Kumar Poonia
 

More from Dr Sandeep Kumar Poonia (20)

Soft computing
Soft computingSoft computing
Soft computing
 
An improved memetic search in artificial bee colony algorithm
An improved memetic search in artificial bee colony algorithmAn improved memetic search in artificial bee colony algorithm
An improved memetic search in artificial bee colony algorithm
 
Modified position update in spider monkey optimization algorithm
Modified position update in spider monkey optimization algorithmModified position update in spider monkey optimization algorithm
Modified position update in spider monkey optimization algorithm
 
Enhanced local search in artificial bee colony algorithm
Enhanced local search in artificial bee colony algorithmEnhanced local search in artificial bee colony algorithm
Enhanced local search in artificial bee colony algorithm
 
RMABC
RMABCRMABC
RMABC
 
Memetic search in differential evolution algorithm
Memetic search in differential evolution algorithmMemetic search in differential evolution algorithm
Memetic search in differential evolution algorithm
 
Improved onlooker bee phase in artificial bee colony algorithm
Improved onlooker bee phase in artificial bee colony algorithmImproved onlooker bee phase in artificial bee colony algorithm
Improved onlooker bee phase in artificial bee colony algorithm
 
Comparative study of_hybrids_of_artificial_bee_colony_algorithm
Comparative study of_hybrids_of_artificial_bee_colony_algorithmComparative study of_hybrids_of_artificial_bee_colony_algorithm
Comparative study of_hybrids_of_artificial_bee_colony_algorithm
 
A novel hybrid crossover based abc algorithm
A novel hybrid crossover based abc algorithmA novel hybrid crossover based abc algorithm
A novel hybrid crossover based abc algorithm
 
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked listsMultiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
 
Sunzip user tool for data reduction using huffman algorithm
Sunzip user tool for data reduction using huffman algorithmSunzip user tool for data reduction using huffman algorithm
Sunzip user tool for data reduction using huffman algorithm
 
New Local Search Strategy in Artificial Bee Colony Algorithm
New Local Search Strategy in Artificial Bee Colony Algorithm New Local Search Strategy in Artificial Bee Colony Algorithm
New Local Search Strategy in Artificial Bee Colony Algorithm
 
A new approach of program slicing
A new approach of program slicingA new approach of program slicing
A new approach of program slicing
 
Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...
 
Enhanced abc algo for tsp
Enhanced abc algo for tspEnhanced abc algo for tsp
Enhanced abc algo for tsp
 
Database aggregation using metadata
Database aggregation using metadataDatabase aggregation using metadata
Database aggregation using metadata
 
Performance evaluation of diff routing protocols in wsn using difft network p...
Performance evaluation of diff routing protocols in wsn using difft network p...Performance evaluation of diff routing protocols in wsn using difft network p...
Performance evaluation of diff routing protocols in wsn using difft network p...
 
Lecture28 tsp
Lecture28 tspLecture28 tsp
Lecture28 tsp
 
Lecture27 linear programming
Lecture27 linear programmingLecture27 linear programming
Lecture27 linear programming
 
Lecture26
Lecture26Lecture26
Lecture26
 

Recently uploaded

STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 

Recently uploaded (20)

STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 

Graph

  • 2. Paths and cycles ● A path is a sequence of nodes v1, v2, …, vN such that (vi,vi+1)E for 0<i<N ■ The length of the path is N-1. ■ Simple path: all vi are distinct, 0<i<N ● A cycle is a path such that v1=vN ■ An acyclic graph has no cycles
  • 4. More useful definitions ● The degree of a vertex is the number of edges incident to that vertex ● In a directed graph: ● The indegree of a node v is the number of distinct edges (w,v)E. ● The outdegree of a node v is the number of distinct edges (v,w)E. ● A node with indegree 0 is a root.
  • 5. Trees are graphs ● A dag is a directed acyclic graph. ● A tree is a connected acyclic undirected graph. ● A forest is an acyclic undirected graph (not necessarily connected), i.e., each connected component is a tree.
  • 6. Even More Terminology ● subgraph: subset of vertices and edges forming a graph ● connected component: maximal connected subgraph. E.g., the graph below has 3 connected components. connected not connected •connected graph: any two vertices are connected by some path
  • 7. 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 
  • 8. More Connectivity n = #vertices m = #edges ● For a tree m = n - 1 n  5 m  4 n  5 m  3 If m < n - 1, G is not connected
  • 9. G = (V, E) a vertex may have: 0 or more predecessors 0 or more successors
  • 10. 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
  • 11. Graphs in Computer Science: How do they help? ● What do they model? ■ An abstraction in Core CS. ○ Examples: VLSI Circuits, Communication Networks, Logical flow of a computer program, Data structures. ■ An abstraction for data and relationships. ○ Examples: The Web, Social Networks, Flows and Flow Networks, Biological Data, Taxonomies, Citations, Explicit relations within a DB system. ● What aspects are studied? ■ Algorithms, Data Structures and Complexity Theory. ■ Characterization and Modeling of Graphs. ■ Implementations of Graph Algorithms in Specific contexts.
  • 12. What is a graph? ● A set of vertices and edges ■ Directed/Undirected ■ Weighted/Unweighted ■ Cyclic/Acyclic vertex edge
  • 13. Representation of Graphs ● Adjacency Matrix ■ A V x V array, with matrix[i][j] storing whether there is an edge between the ith vertex and the jth vertex ● Adjacency Linked List ■ One linked list per vertex, each storing directly reachable vertices
  • 14. Representation of Graphs Adjacency Matrix Adjacency Linked List Memory Storage O(V2) O(V+E) Check whether (u,v) is an edge O(1) O(deg(u)) Find all adjacent vertices of a vertex u O(V) O(deg(u))
  • 15. graph variations ● undirected graph (graph) ■ edges do not have a direction ○ (V1, V2) and (V2, V1) are the same edge ● directed graph (digraph) ■ edges have a direction ○ <V1, V2> and <V2, V1> are different edges ● for either type, edges may be weighted or unweighted
  • 16. a digraph A B C D E V = [A, B, C, D, E] E = [<A,B>, <B,C>, <C,B>, <A,C>, <A,E>, <C,D>]
  • 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)
  • 19. the vertex vector A B C D E 0 1 2 3 4 0 1 2 3 4 A B C D E
  • 20. maximum # edges? a V2 matrix is needed for a graph with V vertices
  • 21. adjacency matrix A B C D E 0 1 2 3 4 0 1 2 3 4 A B C D E 0 1 1 0 1 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 0 1 2 3 4
  • 22. adjacency matrix A B C D A 0 1 1 1 B 1 0 0 1 C 1 0 0 1 D 1 1 1 0
  • 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
  • 24. adjacency lists A B C D E 0 1 2 3 4 0 1 2 3 4 1 2 4 2 1 3 0 1 2 3 4 A B C D E
  • 25. Adjacency Lists A representation of the graph consisting of a list of nodes, with each node containing a list of its neighboring nodes. This representation takes O(|V | + |E|) space.
  • 26. Some graph operations adjacency matrix adjacency lists insertEdge isEdge #successors? #predecessors? O(1) O(1) O(V) O(V) O(e) O(e) O(e) O(E)
  • 27. Introduction ● A free tree is a connected undirected graph without a cycle. ■ Note: This definition of tree is different from the one of a rooted tree ● In a free tree |E| = |V| - 1 ● Example of a free tree: ● A forest is an acyclic directed or undirected graph consisting of two or more trees ● The trees in a directed forest are rooted trees ● The trees in an undirected forest are free trees
  • 29. Use of a stack ● It is very common to use a stack to keep track of: ■ nodes to be visited next, or ■ nodes that we have already visited. ● Typically, use of a stack leads to a depth-first visit order. ● Depth-first visit order is “aggressive” in the sense that it examines complete paths.
  • 30. Use of a queue ● It is very common to use a queue to keep track of: ■ nodes to be visited next, or ■ nodes that we have already visited. ● Typically, use of a queue leads to a breadth- first visit order. ● Breadth-first visit order is “cautious” in the sense that it examines every path of length i before going on to paths of length i+1.
  • 31. Breadth-First Search (BFS) ● BFS tries to search all paths. ● BFS makes use of a queue to store visited (but not dead) vertices, expanding the path from the earliest visited vertices.
  • 32.
  • 33. 1 4 3 2 5 6 Simulation of BFS ● Queue: 1 4 3 5 2 6
  • 34. BFS: Start with Node 5 7 1 5 4 3 2 6 5 1 2 0 4 3 7 6 0
  • 35. BFS: Start with Node 5 7 1 5 4 3 2 6 5 0
  • 37. BFS: Visit 1 and 2 7 1 5 4 3 2 6 5 1 2 0
  • 39. BFS: Visit 0 and 4 7 1 5 4 3 2 6 5 1 2 0 4 0
  • 41. BFS: Visit nodes 3 and 7 7 1 5 4 3 2 6 5 1 2 0 4 3 7 0
  • 43. BFS: Visit 6 7 1 5 4 3 2 6 5 1 2 0 4 3 7 6 0
  • 49. Example ● Breadth-first traversal using a queue. Order of Traversal Queue rearA B D E C G F H I Queue front A B D E C G F H I BFS-tree: Note: The BFS-tree for undirected graph is a free tree
  • 50. Analysis of BFS For a Graph G=(V, E) and n = |V| and m=|E| • When Adjacency List is used Complexity is O(m + n) • When Adjacency Matrix is used  Scanning each row for checking the connectivity of a Vertex is in order O(n). So, Complexity is O(n2)
  • 51. Depth-First Search (DFS) ● Strategy: Go as far as you can (if you have not visit there), otherwise, go back and try another way
  • 52.
  • 53. Example 0 7 1 5 4 3 2 6 Policy: Visit adjacent nodes in increasing index order
  • 54. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4
  • 55. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 Push 5
  • 56. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 Pop/Visit/Mark 5
  • 57. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 2 Push 2, Push 1
  • 58. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 2 Pop/Visit/Mark 1
  • 59. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 2 4 2 Push 4, Push 2, Push 0
  • 60. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 2 4 2 Pop/Visit/Mark 0
  • 61. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 2 4 2 Push 7, Push 3
  • 62. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 2 4 2 Pop/Visit/Mark 3
  • 63. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 2 4 2 Push 2
  • 64. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 2 4 2 Pop/Mark/Visit 2
  • 65. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 2 4 2 Pop/Mark/Visit 7
  • 66. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 2 4 2 Push 6
  • 67. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 2 4 2 Pop/Mark/Visit 6
  • 68. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4 2 Pop (don’t visit) 2
  • 69. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4 2 Pop/Mark/Visit 4
  • 70. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4 Pop (don’t visit) 2
  • 71. Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4 Done
  • 72. Example ● Depth-first traversal using an explicit stack. Order of Traversal StackA B C F E G D H I The Preorder Depth First Tree: Note: The DFS-tree for undirected graph is a free tree
  • 73. Recursive preorder Depth-First Traversal Tracing At each stage, a set of unvisited adjacent vertices of the current vertex is generated. The Preorder Depth First Tree:
  • 74.
  • 75.
  • 76.
  • 77.
  • 78. Analysis of DFS For a Graph G=(V, E) and n = |V| and m=|E| • When Adjacency List is used Complexity is O(m + n) • When Adjacency Matrix is used  Scanning each row for checking the connectivity of a Vertex is in order O(n). So, Complexity is O(n2) DFS uses space O(|V|) in the worst case to store the stack of vertices on the current search path as well as the set of already-visited vertices.