SlideShare a Scribd company logo
1 of 62
Unit VI
Graphs
Graph
• A non-linear data structure consisting of vertices and edges.
G =(V,E)
where V= set of Vertices
E = set of Edges
• A Graph consists of finite set of vertices and set of edges which
connect a pair of node.
Terminologies
• Vertex − Each node of the graph is represented as a vertex.
• Edge − Edge represents a path between two vertices or a line
between two vertices.
• Adjacency − Two node or vertices are adjacent if they are connected
to each other through an edge.
• Path − Path represents a sequence of edges between the two
vertices.
• Degree of a node – is the number of edges the node is used to define
• In – degree – Number of edges pointing to a node
• Out – degree – Number of edges pointing from a node
Terminologies
• Cycle – a path that starts and ends on the same vertex.
• Simple path – a path that does not cross itself.
• Length of a path: Number of edges in the path.
• An undirected graph is connected if every pair of vertices has a path
between it.
• A directed graph is strongly connected if every pair of vertices has a
path between them, in both directions
Types of Graphs
• Weighted Graph - called a labeled or weighted graph. Edges have a
weight that typically shows cost of traversing.
• Unweighted graph: edges have no weight. Edges simply show
connections.
Types of Graphs
• Directed Graphs: each edge can be traversed only in a specified
direction.
• Undirected Graphs: each edge can be traversed in either direction
Types of Graphs
• A Cyclic graph contains cycles.
• An acyclic graph contains no cycles.
Applications ofgraphs
• To represent networks. The networks may include paths in a city or
telephone network or circuit network.
• In social networks like linkedIn, Facebook. For example, in Facebook,
each person is represented with a vertex(or node). Each node is a
structure and contains information like person id, name, gender, etc.
Applications ofgraphs
• Electrical Engineering − extensively used in designing circuit connections.
• Computer Network − The relationships among interconnected computers
in the network follows the principles of graph theory.
• Science − The molecular structure and chemical structure of a substance,
the DNA structure of an organism, etc., are represented by graphs.
• Linguistics − The parsing tree of a language and grammar of a language
uses graphs.
Graphs
• Graphs can also be defined in the form of matrices.
• To perform the calculation of paths and cycles in the graphs, matrix
representation is used.
• It is calculated using matrix operations.
• The two most common representation of the graphs are:
• Adjacency Matrix
• Adjacency List
AdjacencyMatrix
• is nothing but a square matrix utilised to describe a finite graph.
• also called the connection matrix.
• is a matrix containing rows and columns which is used to represent a
simple labelled graph, with 0 or 1.
• If a graph has n number of vertices, then the adjacency matrix
of that graph is n x n, and each entry of the matrix represents
the number of edges from one vertex to another.
• Sometimes it is also called a Vertex matrix.
Adjacency matrix for an undirected graph
• edges are not associated with the directions with them.
Adjacency matrix for a directed graph
Adjacency matrix for the undirected weighted graph
Adjacency matrix for the directed weighted graph
Adjacency List
• a list that helps you keep track each node’s neighbor in a graph.
• is an array of separate lists.
• Each element of array is a list of corresponding neighbour (or directly
connected) vertices.
• This representation is based on Linked Lists.
• At the end of list, each node is connected with the null values to tell
that it is the end node of that list.
Adjacency List
• Undirected graph
• Directed Graph
Operations onGraphs
• Add/Remove Vertex
• Add/Remove Edge
• Traverse a graph
Add Vertex
• the graph's size grows by one, increasing the matrix's size by one at
the row and column levels.
Remove Vertex
• the graph's size shrinks by one, decreasing the matrix's size by one at
the row and column levels.
Add/Remove Edge
• Connecting two provided vertices can be used to add an edge to a
graph.
Remove Edge
• The connection between the vertices or nodes can be removed to
delete an edge
Traverse a graph
• The process of visiting or updating each vertex in a graph is known as
graph traversal.
• The sequence in which they visit the vertices is used to classify such
traversals. Graph traversal is a subset of tree traversal.
• There are two techniques to implement a graph traversal algorithm:
• Breadth-first search
• Depth-first search
Breadth-First Search (BFS)
• a search technique for finding a node in a graph data structure that meets a set of
criteria.
• It begins at the root of the graph and investigates all nodes at the current
depth level before moving on to nodes at the next depth level.
• To maintain track of the child nodes that have been encountered but not yet
inspected, more memory, generally a queue is required.
Breadth-First Traversal
• a graph is similar to Breadth-First Traversal of a tree.
• graphs may contain cycles, the same node may be visited again.
• To avoid processing a node more than once, the vertices are divided
into two categories:
• Visited
• Not visited
Breadth-First Search Algorithm
• starts at the first starting node in a graph and travels it entirely.
• After traversing the first node successfully, it visits and marks the next
non-traversed vertex in the graph.
• in the graph, every node is known.
• initialize a queue.
• start from source node and mark it as visited.
• observe unvisited nearby nodes, mark it as visited, and enqueue it
alphabetically.
• If a given source node has no unvisited nodes in its immediate vicinity
dequeue it.
BFS algorithm
• First, add A to queue1 and NULL to
queue2
• Now, delete node A from queue1
and add it into queue2. Insert all
neighbors of node A to queue1.
BFS algorithm
• First, add A to queue1 and NULL to
queue2
• Now, delete node A from queue1
and add it into queue2. Insert all
neighbors of node A to queue1.
BFS algorithm
• First, add A to queue1 and NULL to
queue2
• Now, delete node A from queue1
and add it into queue2. Insert all
neighbors of node A to queue1.
BFS algorithm
• First, add A to queue1 and NULL to
queue2
• Now, delete node A from queue1
and add it into queue2. Insert all
neighbors of node A to queue1.
BFS algorithm
• First, add A to queue1 and NULL to
queue2
• Now, delete node A from queue1
and add it into queue2. Insert all
neighbors of node A to queue1.
BFS algorithm
• First, add A to queue1 and NULL to
queue2
• Now, delete node A from queue1
and add it into queue2. Insert all
neighbors of node A to queue1.
BFS algorithm
• First, add A to queue1 and NULL to
queue2
• Now, delete node A from queue1
and add it into queue2. Insert all
neighbors of node A to queue1.
Complexity of BFS algorithm
• The time complexity of BFS algorithm is O(V+E).
• The space complexity of BFS can be expressed as O(V).
Applications of BFS
• For GPS navigation
• Path finding algorithms
• In Ford-Fulkerson algorithm to find maximum flow in a network
• Cycle detection in an undirected graph
• In minimum spanning tree
Depth-First Search (DFS)
• a search technique for finding a node in a graph data structure that meets a set of
criteria.
• The depth-first search (DFS) algorithm traverses or explores data structures
such as trees and graphs. The DFS algorithm begins at the root node and
examines each branch as far as feasible before backtracking.
• To maintain track of the child nodes that have been encountered but not yet
inspected, more memory, generally a stack is required.
Depth-First Search (DFS)
• It is a recursive algorithm to search all the vertices of a tree data structure or a
graph.
• The depth-first search (DFS) algorithm starts with the initial node of graph and
goes deeper until the goal node or the node with no children is reached.
• Because of the recursive nature, stack data structure can be used to implement
the DFS algorithm.
DFS traversal
• First, create a stack with the total number of vertices in the graph.
• Now, choose any vertex as the starting point of traversal, and push that vertex
into the stack.
• After that, push a non-visited vertex (adjacent to the vertex on the top of the
stack) to the top of the stack.
• Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex on the
stack's top.
• If no vertex is left, go back and pop a vertex from the stack.
• Repeat steps 2, 3, and 4 until the stack is empty.
DFS traversal
• First, push H onto the stack.
DFS traversal
• First, push H onto the stack.
DFS traversal
• First, push H onto the stack.
DFS traversal
• First, push H onto the stack.
DFS traversal
• First, push H onto the stack.
DFS traversal
• First, push H onto the stack.
DFS traversal
• First, push H onto the stack.
DFS traversal
• First, push H onto the stack.
Complexity of Depth-first search algorithm
• The time complexity of the DFS algorithm is O(V+E)
• The space complexity of the DFS algorithm is O(V)
Applications of DFS algorithm
• For finding the path.
• For finding the strongly connected components of a graph.
• For detecting cycles in a graph.
Spanning Tree
• A spanning tree is a subset of Graph G.
• It has all the vertices covered with minimum possible number of
edges.
• It does not have cycles and it cannot be disconnected.
• Spanning tree is basically used to find a minimum path to connect all
nodes in a graph.
Properties of Spanning Tree
• A connected graph G can have more than one spanning tree.
• All possible spanning trees of graph G, have the same number of edges and
vertices.
• The spanning tree does not have any cycle (loops).
• Removing one edge from the spanning tree will make the graph disconnected, i.e.
the spanning tree is minimally connected.
• Adding one edge to the spanning tree will create a circuit or loop, i.e. the
spanning tree is maximally acyclic.
Mathematical Properties of Spanning Tree
• Spanning tree has n-1 edges, where n is the number of nodes
(vertices).
• A spanning tree can be constructed from a complete graph, by
removing maximum e - n + 1 edges.
• A complete graph can have maximum nn-2 number of spanning trees.
Application of Spanning Tree
• Civil Network Planning
• Computer Network Routing Protocol
• Cluster Analysis
Minimum Spanning Tree (MST)
• A spanning tree that has minimum weight than all other spanning
trees of the same graph.
• This weight can be measured as distance, congestion, traffic load or
any arbitrary value denoted to the edges.
Applications of minimum spanning tree
• Minimum spanning tree can be used to design water-supply
networks, telecommunication networks, and electrical grids.
• It can be used to find paths in the map.
Minimum Spanning-Tree Algorithm
• Kruskal's Algorithm
• Prim's Algorithm
Kruskal's Algorithm
• Used to find the minimum cost spanning tree using the greedy
approach.
• Treats the graph as a forest and every node it has as an individual
tree.
• A tree connects to another only and only if, it has the least cost
among all available options and does not violate MST properties.
Kruskal's Algorithm
• Used to find the minimum cost spanning tree using the greedy
approach.
• Edge with minimum weight is selected.
• Cycle should not be formed.
• Each time the edge of minimum weight has to be selected.
• It is not necessary to have edges of minimum weights to be adjacent.
Kruskal's Algorithm
Remove all loops and Parallel Edges - In case of parallel edges, keep the one which has
the least cost associated and remove all others.
Kruskal's Algorithm
• Arrange all edges in their increasing order of weight - create a set of edges and
weight, and arrange them in an ascending order of weightage (cost).
• Add the edge which has the least weightage - start adding edges to the graph
beginning from the one which has the least weight while checking that the spanning
properties remain intact.
Kruskal's Algorithm
1
5
4
3
2 6
Kruskal's Algorithm
Remove all loops and Parallel Edges - In case of parallel edges, keep the one which has
the least cost associated and remove all others.

More Related Content

Similar to Unit VI - Graphs.ppt (20)

Graph Algorithms
Graph AlgorithmsGraph Algorithms
Graph Algorithms
 
Graphs (1)
Graphs (1)Graphs (1)
Graphs (1)
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Graphs data structures
Graphs data structuresGraphs data structures
Graphs data structures
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Graph 1
Graph 1Graph 1
Graph 1
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Techniques for graph
Techniques for graphTechniques for graph
Techniques for graph
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Graph & Heap in Data Structure (Basic Information)
Graph & Heap in Data Structure (Basic Information)Graph & Heap in Data Structure (Basic Information)
Graph & Heap in Data Structure (Basic Information)
 
Daa chpater 12
Daa chpater 12Daa chpater 12
Daa chpater 12
 
graphs.ppt
graphs.pptgraphs.ppt
graphs.ppt
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
 
Graph
GraphGraph
Graph
 
Bfs dfs mst
Bfs dfs mstBfs dfs mst
Bfs dfs mst
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdf
 
DS ppt
DS pptDS ppt
DS ppt
 

More from HODElex

Unit 4_Network Layer_Part II.pptx
Unit 4_Network Layer_Part II.pptxUnit 4_Network Layer_Part II.pptx
Unit 4_Network Layer_Part II.pptxHODElex
 
unit II_2_i.pptx
unit II_2_i.pptxunit II_2_i.pptx
unit II_2_i.pptxHODElex
 
IEEE and Lower Level LAN Protocols.ppt
IEEE and Lower Level LAN Protocols.pptIEEE and Lower Level LAN Protocols.ppt
IEEE and Lower Level LAN Protocols.pptHODElex
 
I & E Tutorials.pdf
I & E Tutorials.pdfI & E Tutorials.pdf
I & E Tutorials.pdfHODElex
 
wireless lan presentation.ppt
wireless lan presentation.pptwireless lan presentation.ppt
wireless lan presentation.pptHODElex
 
Unit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.pptUnit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.pptHODElex
 
Unit 3_Network Layer_Part II.pptx
Unit 3_Network Layer_Part II.pptxUnit 3_Network Layer_Part II.pptx
Unit 3_Network Layer_Part II.pptxHODElex
 

More from HODElex (7)

Unit 4_Network Layer_Part II.pptx
Unit 4_Network Layer_Part II.pptxUnit 4_Network Layer_Part II.pptx
Unit 4_Network Layer_Part II.pptx
 
unit II_2_i.pptx
unit II_2_i.pptxunit II_2_i.pptx
unit II_2_i.pptx
 
IEEE and Lower Level LAN Protocols.ppt
IEEE and Lower Level LAN Protocols.pptIEEE and Lower Level LAN Protocols.ppt
IEEE and Lower Level LAN Protocols.ppt
 
I & E Tutorials.pdf
I & E Tutorials.pdfI & E Tutorials.pdf
I & E Tutorials.pdf
 
wireless lan presentation.ppt
wireless lan presentation.pptwireless lan presentation.ppt
wireless lan presentation.ppt
 
Unit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.pptUnit II_Searching and Sorting Algorithms.ppt
Unit II_Searching and Sorting Algorithms.ppt
 
Unit 3_Network Layer_Part II.pptx
Unit 3_Network Layer_Part II.pptxUnit 3_Network Layer_Part II.pptx
Unit 3_Network Layer_Part II.pptx
 

Recently uploaded

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 

Recently uploaded (20)

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 

Unit VI - Graphs.ppt

  • 2. Graph • A non-linear data structure consisting of vertices and edges. G =(V,E) where V= set of Vertices E = set of Edges • A Graph consists of finite set of vertices and set of edges which connect a pair of node.
  • 3. Terminologies • Vertex − Each node of the graph is represented as a vertex. • Edge − Edge represents a path between two vertices or a line between two vertices. • Adjacency − Two node or vertices are adjacent if they are connected to each other through an edge. • Path − Path represents a sequence of edges between the two vertices. • Degree of a node – is the number of edges the node is used to define • In – degree – Number of edges pointing to a node • Out – degree – Number of edges pointing from a node
  • 4. Terminologies • Cycle – a path that starts and ends on the same vertex. • Simple path – a path that does not cross itself. • Length of a path: Number of edges in the path. • An undirected graph is connected if every pair of vertices has a path between it. • A directed graph is strongly connected if every pair of vertices has a path between them, in both directions
  • 5. Types of Graphs • Weighted Graph - called a labeled or weighted graph. Edges have a weight that typically shows cost of traversing. • Unweighted graph: edges have no weight. Edges simply show connections.
  • 6. Types of Graphs • Directed Graphs: each edge can be traversed only in a specified direction. • Undirected Graphs: each edge can be traversed in either direction
  • 7. Types of Graphs • A Cyclic graph contains cycles. • An acyclic graph contains no cycles.
  • 8. Applications ofgraphs • To represent networks. The networks may include paths in a city or telephone network or circuit network. • In social networks like linkedIn, Facebook. For example, in Facebook, each person is represented with a vertex(or node). Each node is a structure and contains information like person id, name, gender, etc.
  • 9. Applications ofgraphs • Electrical Engineering − extensively used in designing circuit connections. • Computer Network − The relationships among interconnected computers in the network follows the principles of graph theory. • Science − The molecular structure and chemical structure of a substance, the DNA structure of an organism, etc., are represented by graphs. • Linguistics − The parsing tree of a language and grammar of a language uses graphs.
  • 10. Graphs • Graphs can also be defined in the form of matrices. • To perform the calculation of paths and cycles in the graphs, matrix representation is used. • It is calculated using matrix operations. • The two most common representation of the graphs are: • Adjacency Matrix • Adjacency List
  • 11. AdjacencyMatrix • is nothing but a square matrix utilised to describe a finite graph. • also called the connection matrix. • is a matrix containing rows and columns which is used to represent a simple labelled graph, with 0 or 1. • If a graph has n number of vertices, then the adjacency matrix of that graph is n x n, and each entry of the matrix represents the number of edges from one vertex to another. • Sometimes it is also called a Vertex matrix.
  • 12. Adjacency matrix for an undirected graph • edges are not associated with the directions with them.
  • 13. Adjacency matrix for a directed graph
  • 14. Adjacency matrix for the undirected weighted graph
  • 15. Adjacency matrix for the directed weighted graph
  • 16. Adjacency List • a list that helps you keep track each node’s neighbor in a graph. • is an array of separate lists. • Each element of array is a list of corresponding neighbour (or directly connected) vertices. • This representation is based on Linked Lists. • At the end of list, each node is connected with the null values to tell that it is the end node of that list.
  • 17. Adjacency List • Undirected graph • Directed Graph
  • 18. Operations onGraphs • Add/Remove Vertex • Add/Remove Edge • Traverse a graph
  • 19. Add Vertex • the graph's size grows by one, increasing the matrix's size by one at the row and column levels.
  • 20. Remove Vertex • the graph's size shrinks by one, decreasing the matrix's size by one at the row and column levels.
  • 21. Add/Remove Edge • Connecting two provided vertices can be used to add an edge to a graph.
  • 22. Remove Edge • The connection between the vertices or nodes can be removed to delete an edge
  • 23. Traverse a graph • The process of visiting or updating each vertex in a graph is known as graph traversal. • The sequence in which they visit the vertices is used to classify such traversals. Graph traversal is a subset of tree traversal. • There are two techniques to implement a graph traversal algorithm: • Breadth-first search • Depth-first search
  • 24. Breadth-First Search (BFS) • a search technique for finding a node in a graph data structure that meets a set of criteria. • It begins at the root of the graph and investigates all nodes at the current depth level before moving on to nodes at the next depth level. • To maintain track of the child nodes that have been encountered but not yet inspected, more memory, generally a queue is required.
  • 25. Breadth-First Traversal • a graph is similar to Breadth-First Traversal of a tree. • graphs may contain cycles, the same node may be visited again. • To avoid processing a node more than once, the vertices are divided into two categories: • Visited • Not visited
  • 26. Breadth-First Search Algorithm • starts at the first starting node in a graph and travels it entirely. • After traversing the first node successfully, it visits and marks the next non-traversed vertex in the graph. • in the graph, every node is known. • initialize a queue. • start from source node and mark it as visited. • observe unvisited nearby nodes, mark it as visited, and enqueue it alphabetically. • If a given source node has no unvisited nodes in its immediate vicinity dequeue it.
  • 27. BFS algorithm • First, add A to queue1 and NULL to queue2 • Now, delete node A from queue1 and add it into queue2. Insert all neighbors of node A to queue1.
  • 28. BFS algorithm • First, add A to queue1 and NULL to queue2 • Now, delete node A from queue1 and add it into queue2. Insert all neighbors of node A to queue1.
  • 29. BFS algorithm • First, add A to queue1 and NULL to queue2 • Now, delete node A from queue1 and add it into queue2. Insert all neighbors of node A to queue1.
  • 30. BFS algorithm • First, add A to queue1 and NULL to queue2 • Now, delete node A from queue1 and add it into queue2. Insert all neighbors of node A to queue1.
  • 31. BFS algorithm • First, add A to queue1 and NULL to queue2 • Now, delete node A from queue1 and add it into queue2. Insert all neighbors of node A to queue1.
  • 32. BFS algorithm • First, add A to queue1 and NULL to queue2 • Now, delete node A from queue1 and add it into queue2. Insert all neighbors of node A to queue1.
  • 33. BFS algorithm • First, add A to queue1 and NULL to queue2 • Now, delete node A from queue1 and add it into queue2. Insert all neighbors of node A to queue1.
  • 34. Complexity of BFS algorithm • The time complexity of BFS algorithm is O(V+E). • The space complexity of BFS can be expressed as O(V).
  • 35. Applications of BFS • For GPS navigation • Path finding algorithms • In Ford-Fulkerson algorithm to find maximum flow in a network • Cycle detection in an undirected graph • In minimum spanning tree
  • 36. Depth-First Search (DFS) • a search technique for finding a node in a graph data structure that meets a set of criteria. • The depth-first search (DFS) algorithm traverses or explores data structures such as trees and graphs. The DFS algorithm begins at the root node and examines each branch as far as feasible before backtracking. • To maintain track of the child nodes that have been encountered but not yet inspected, more memory, generally a stack is required.
  • 37. Depth-First Search (DFS) • It is a recursive algorithm to search all the vertices of a tree data structure or a graph. • The depth-first search (DFS) algorithm starts with the initial node of graph and goes deeper until the goal node or the node with no children is reached. • Because of the recursive nature, stack data structure can be used to implement the DFS algorithm.
  • 38. DFS traversal • First, create a stack with the total number of vertices in the graph. • Now, choose any vertex as the starting point of traversal, and push that vertex into the stack. • After that, push a non-visited vertex (adjacent to the vertex on the top of the stack) to the top of the stack. • Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex on the stack's top. • If no vertex is left, go back and pop a vertex from the stack. • Repeat steps 2, 3, and 4 until the stack is empty.
  • 39. DFS traversal • First, push H onto the stack.
  • 40. DFS traversal • First, push H onto the stack.
  • 41. DFS traversal • First, push H onto the stack.
  • 42. DFS traversal • First, push H onto the stack.
  • 43. DFS traversal • First, push H onto the stack.
  • 44. DFS traversal • First, push H onto the stack.
  • 45. DFS traversal • First, push H onto the stack.
  • 46. DFS traversal • First, push H onto the stack.
  • 47. Complexity of Depth-first search algorithm • The time complexity of the DFS algorithm is O(V+E) • The space complexity of the DFS algorithm is O(V)
  • 48. Applications of DFS algorithm • For finding the path. • For finding the strongly connected components of a graph. • For detecting cycles in a graph.
  • 49. Spanning Tree • A spanning tree is a subset of Graph G. • It has all the vertices covered with minimum possible number of edges. • It does not have cycles and it cannot be disconnected. • Spanning tree is basically used to find a minimum path to connect all nodes in a graph.
  • 50. Properties of Spanning Tree • A connected graph G can have more than one spanning tree. • All possible spanning trees of graph G, have the same number of edges and vertices. • The spanning tree does not have any cycle (loops). • Removing one edge from the spanning tree will make the graph disconnected, i.e. the spanning tree is minimally connected. • Adding one edge to the spanning tree will create a circuit or loop, i.e. the spanning tree is maximally acyclic.
  • 51. Mathematical Properties of Spanning Tree • Spanning tree has n-1 edges, where n is the number of nodes (vertices). • A spanning tree can be constructed from a complete graph, by removing maximum e - n + 1 edges. • A complete graph can have maximum nn-2 number of spanning trees.
  • 52. Application of Spanning Tree • Civil Network Planning • Computer Network Routing Protocol • Cluster Analysis
  • 53. Minimum Spanning Tree (MST) • A spanning tree that has minimum weight than all other spanning trees of the same graph. • This weight can be measured as distance, congestion, traffic load or any arbitrary value denoted to the edges.
  • 54. Applications of minimum spanning tree • Minimum spanning tree can be used to design water-supply networks, telecommunication networks, and electrical grids. • It can be used to find paths in the map.
  • 55. Minimum Spanning-Tree Algorithm • Kruskal's Algorithm • Prim's Algorithm
  • 56. Kruskal's Algorithm • Used to find the minimum cost spanning tree using the greedy approach. • Treats the graph as a forest and every node it has as an individual tree. • A tree connects to another only and only if, it has the least cost among all available options and does not violate MST properties.
  • 57. Kruskal's Algorithm • Used to find the minimum cost spanning tree using the greedy approach. • Edge with minimum weight is selected. • Cycle should not be formed. • Each time the edge of minimum weight has to be selected. • It is not necessary to have edges of minimum weights to be adjacent.
  • 58. Kruskal's Algorithm Remove all loops and Parallel Edges - In case of parallel edges, keep the one which has the least cost associated and remove all others.
  • 59. Kruskal's Algorithm • Arrange all edges in their increasing order of weight - create a set of edges and weight, and arrange them in an ascending order of weightage (cost). • Add the edge which has the least weightage - start adding edges to the graph beginning from the one which has the least weight while checking that the spanning properties remain intact.
  • 61.
  • 62. Kruskal's Algorithm Remove all loops and Parallel Edges - In case of parallel edges, keep the one which has the least cost associated and remove all others.