Basic Graph Algorithms Vertex (Node): A fundamental unit of a graph, representing an entity (e.g., a person in a social network, a city in a transportation network).
Edge: A connection between two vertices, representing a relationship (e.g., a friendship, a road between cities).
The document discusses optimization problems and various graph search algorithms. It covers:
- Formulating optimization problems as finding the best solution from all feasible solutions.
- Graph search algorithms like breadth-first search and depth-first search that can be used to find optimal solutions by traversing graphs.
- Dijkstra's algorithm, a graph search algorithm that finds the shortest paths between nodes in a graph.
Breath first Search and Depth first searchKirti Verma
油
The document discusses graph traversal algorithms depth-first search (DFS) and breadth-first search (BFS). DFS uses a stack and traverses deeper nodes before shallower ones, outputting different traversal orders depending on the starting node. BFS uses a queue and traverses all neighbors of a node before moving to the next level, always outputting the same traversal order. Examples are given of applying DFS and BFS to a sample graph. Applications of the algorithms include computing distances, checking for cycles, and determining reachability between nodes.
The document discusses the Breadth-First Search (BFS) algorithm. It begins with an introduction to graph traversal and explains that BFS is a graph traversal technique that explores all nodes layer-by-layer from a starting node. It then provides an example of applying BFS to a binary tree, showing the steps of selecting a starting node, inserting its children into a queue, extracting nodes from the queue and inserting their children, and repeating until the queue is empty. The document concludes by listing some applications of BFS such as for web crawlers, GPS navigation, and finding the minimum spanning tree of an unweighted graph.
Topological sorting is a linear ordering of the vertices in a directed acyclic graph (DAG) where for every edge from vertex u to vertex v, u comes before v in the ordering. The algorithm uses a depth-first search approach with a stack to iteratively push and pop vertices as their adjacent vertices are explored. Breadth-first search also traverses a graph by exploring the neighbor vertices, but uses a queue rather than a stack and marks visited vertices to avoid getting stuck in cycles. The document provides pseudocode for both algorithms with an example graph to demonstrate the process.
This document provides an outline and overview of a lecture on elementary graph algorithms. It begins with contact information for the lecturer, Dr. Muhammad Hanif Durad. It then outlines topics to be covered, including definition and representation of graphs, breadth-first search, depth-first search, topological sort, and strongly connected components. The document discusses the importance of graphs and examples of problems that can be modeled with graphs. It provides definitions and descriptions of basic graph terminology like vertices, edges, types of graphs. It also covers representations of graphs using adjacency lists and adjacency matrices. The document dives deeper into breadth-first search and depth-first search algorithms, providing pseudocode and examples. It discusses applications and analysis of the algorithms.
This document discusses algorithms for finding the maximum and minimum elements in an array, including the straightforward method that requires 2n-2 comparisons and the divide-and-conquer method that requires fewer comparisons. It also covers graph traversal algorithms like breadth-first search (BFS) and depth-first search (DFS), and discusses applications like finding articulation points in a graph. Examples of applying BFS, DFS, and the algorithm for finding articulation points using DFS are provided.
An overview of the most simple algorithms used in data structures for path finding. Dijkstra, Breadth First Search, Depth First Search, Best First Search and A-star
Naturally feel free to copy for assignments and all
The document defines and explains several key graph concepts and algorithms, including:
- Graph representations like adjacency matrix and adjacency list.
- Graph traversal algorithms like breadth-first search (BFS) and depth-first search (DFS).
- Minimum spanning tree algorithms like Prim's algorithm.
- Single-source shortest path algorithms like Dijkstra's algorithm and Floyd's algorithm.
Pseudocode and examples are provided to illustrate how BFS, DFS, Prim's, Dijkstra's and Floyd's algorithms work on graphs. Key properties of minimum spanning trees and shortest path problems are also defined.
Algorithm Design and Complexity - Course 7Traian Rebedea
油
The document discusses algorithms for graphs, including breadth-first search (BFS) and depth-first search (DFS). BFS uses a queue to traverse nodes level-by-level from a starting node, computing the shortest path. DFS uses a stack, exploring as far as possible along each branch before backtracking, and computes discovery and finish times for nodes. Both algorithms color nodes white, gray, black to track explored status and maintain predecessor pointers to reconstruct paths. Common graph representations like adjacency lists and matrices are also covered.
Depth First Search and Breadth First SearchNisha Soms
油
The document defines and provides examples of key concepts related to graphs and graph traversal algorithms. It describes what a graph is comprised of (vertices and edges), common graph types (directed/undirected, weighted/unweighted), graph terminology (degree, path, cycle, connectivity), and graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS). It provides pseudocode for DFS and BFS algorithms and analyzes their runtime and ability to find optimal paths.
Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at a root node and explores all neighboring nodes. Then it explores the unexplored neighbors of the newly discovered nodes, and so on, exploring one "shell" of neighbors at a time. The key steps are: 1) Enqueue the root node in a queue. 2) Dequeue the first node and explore its unexplored neighbors, enqueueing them. 3) Repeat until the queue is empty, exploring one level of neighbors at a time.
The document discusses graph traversal algorithms breadth-first search (BFS) and depth-first search (DFS). It provides examples of how BFS and DFS work, including pseudocode for algorithms. It also discusses applications of BFS such as finding shortest paths and detecting bipartitions. Applications of DFS include finding connected components and topological sorting.
The document discusses depth-first search (DFS) and breadth-first search (BFS) graph traversal algorithms. It provides details on how DFS uses a stack to search deeper in the graph first by exploring edges from the most recently discovered vertex. BFS uses a queue to search outward from the starting node to neighboring nodes first before searching further. An example of DFS running on a graph is shown with each step of the algorithm.
Graphs are data structures consisting of nodes and edges connecting nodes. They can be directed or undirected. Trees are special types of graphs. Common graph algorithms include depth-first search (DFS) and breadth-first search (BFS). DFS prioritizes exploring nodes along each branch as deeply as possible before backtracking, using a stack. BFS explores all nodes at the current depth before moving to the next depth, using a queue.
Analysis and design of algorithms part 3Deepak John
油
This document discusses graph representation and traversal techniques. It begins by defining graph terminology like vertices, edges, adjacency, and paths. It then covers different ways to represent graphs, including adjacency lists and matrices. It describes the pros and cons of each representation. The document also explains depth-first search and breadth-first search traversal algorithms in detail, including pseudocode. It analyzes the time complexity of these algorithms. Finally, it briefly discusses other graph topics like strongly connected components and biconnected components.
This document discusses graphs and graph algorithms. It defines what a graph is - a set of vertices connected by edges. It covers different types of graphs like directed/undirected graphs and weighted graphs. It then explains two common graph search algorithms - depth-first search (DFS) and breadth-first search (BFS). DFS explores each path as deep as possible before backtracking while BFS explores all neighbors of a node before moving deeper. Both algorithms run in O(V+E) time where V is vertices and E is edges. BFS always finds the shortest path while DFS is not guaranteed to.
The document discusses graphs and graph algorithms. It begins by defining what a graph is - a collection of vertices connected by edges. It then lists four learning objectives related to representing graphs, traversing graphs, calculating minimum spanning trees, and finding shortest routes. The document goes on to describe different ways of representing graphs through adjacency matrices and lists. It also explains graph traversal algorithms like depth-first search and breadth-first search. Finally, it discusses algorithms for finding minimum spanning trees and shortest paths in weighted graphs.
Algorithms and data Chapter 3 V Graph.pptxzerihunnana
油
This document discusses graphs and graph algorithms. It defines graphs as collections of vertices and edges. It describes different types of graphs like directed, undirected, weighted graphs. It explains graph traversal algorithms like breadth-first search and depth-first search. It also discusses minimum spanning trees and algorithms to find them, like Prim's algorithm and Kruskal's algorithm.
The document discusses graphs and graph algorithms. It defines what a graph is and how they can be represented. It also explains graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS). Additionally, it covers algorithms for finding the shortest path using Dijkstra's algorithm and calculating minimum spanning trees using Kruskal's algorithm.
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...RVSPSOA
油
Principles of statics. Forces and their effects. Types of force systems. Resultant of concurrent and
parallel forces. Lamis theorem. Principle of moments. Varignons theorem. Principle of equilibrium.
Types of supports and reactions-Bending moment and Shear forces-Determination of reactions for
simply supported beams. Relation between bending moment and shear force.
Properties of section Centre of gravity, Moment of Inertia, Section modulus, Radius of gyration
for various structural shapes. Theorem of perpendicular axis. Theorem of parallel axis.
Elastic properties of solids. Concept of stress and strain. Deformation of axially loaded simple bars.
Types of stresses. Concept of axial and volumetric stresses and strains. Elastic constants. Elastic
Modulus. Shear Modulus. Bulk Modulus. Poissons ratio. Relation between elastic constants.
Principal stresses and strain. Numerical and Graphical method. Mohrs diagram.
R.K. Bansal, A Text book on Engineering Mechanics, Lakshmi Publications, Delhi,2008.
R.K. Bansal, A textbook on Strength of Materials, Lakshmi Publications, Delhi 2010.
Paul W. McMullin, 'Jonathan S. Price, Introduction to Structures, Routledge, 2016.
P.C. Punmia, Strength of Materials and Theory of Structures; Vol. I, Lakshmi
Publications, Delhi 2018.
2. S. Ramamrutham, Strength of Materials, Dhanpatrai and Sons, Delhi, 2014.
3. W.A. Nash, Strength of Materials, Schaums Series, McGraw Hill Book Company,1989.
4. R.K. Rajput, Strength of Materials, S.K. Kataria and Sons, New Delhi , 2017.
More Related Content
Similar to Lec 15-graph Searching in data structure and lgorithm.pptx (20)
An overview of the most simple algorithms used in data structures for path finding. Dijkstra, Breadth First Search, Depth First Search, Best First Search and A-star
Naturally feel free to copy for assignments and all
The document defines and explains several key graph concepts and algorithms, including:
- Graph representations like adjacency matrix and adjacency list.
- Graph traversal algorithms like breadth-first search (BFS) and depth-first search (DFS).
- Minimum spanning tree algorithms like Prim's algorithm.
- Single-source shortest path algorithms like Dijkstra's algorithm and Floyd's algorithm.
Pseudocode and examples are provided to illustrate how BFS, DFS, Prim's, Dijkstra's and Floyd's algorithms work on graphs. Key properties of minimum spanning trees and shortest path problems are also defined.
Algorithm Design and Complexity - Course 7Traian Rebedea
油
The document discusses algorithms for graphs, including breadth-first search (BFS) and depth-first search (DFS). BFS uses a queue to traverse nodes level-by-level from a starting node, computing the shortest path. DFS uses a stack, exploring as far as possible along each branch before backtracking, and computes discovery and finish times for nodes. Both algorithms color nodes white, gray, black to track explored status and maintain predecessor pointers to reconstruct paths. Common graph representations like adjacency lists and matrices are also covered.
Depth First Search and Breadth First SearchNisha Soms
油
The document defines and provides examples of key concepts related to graphs and graph traversal algorithms. It describes what a graph is comprised of (vertices and edges), common graph types (directed/undirected, weighted/unweighted), graph terminology (degree, path, cycle, connectivity), and graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS). It provides pseudocode for DFS and BFS algorithms and analyzes their runtime and ability to find optimal paths.
Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at a root node and explores all neighboring nodes. Then it explores the unexplored neighbors of the newly discovered nodes, and so on, exploring one "shell" of neighbors at a time. The key steps are: 1) Enqueue the root node in a queue. 2) Dequeue the first node and explore its unexplored neighbors, enqueueing them. 3) Repeat until the queue is empty, exploring one level of neighbors at a time.
The document discusses graph traversal algorithms breadth-first search (BFS) and depth-first search (DFS). It provides examples of how BFS and DFS work, including pseudocode for algorithms. It also discusses applications of BFS such as finding shortest paths and detecting bipartitions. Applications of DFS include finding connected components and topological sorting.
The document discusses depth-first search (DFS) and breadth-first search (BFS) graph traversal algorithms. It provides details on how DFS uses a stack to search deeper in the graph first by exploring edges from the most recently discovered vertex. BFS uses a queue to search outward from the starting node to neighboring nodes first before searching further. An example of DFS running on a graph is shown with each step of the algorithm.
Graphs are data structures consisting of nodes and edges connecting nodes. They can be directed or undirected. Trees are special types of graphs. Common graph algorithms include depth-first search (DFS) and breadth-first search (BFS). DFS prioritizes exploring nodes along each branch as deeply as possible before backtracking, using a stack. BFS explores all nodes at the current depth before moving to the next depth, using a queue.
Analysis and design of algorithms part 3Deepak John
油
This document discusses graph representation and traversal techniques. It begins by defining graph terminology like vertices, edges, adjacency, and paths. It then covers different ways to represent graphs, including adjacency lists and matrices. It describes the pros and cons of each representation. The document also explains depth-first search and breadth-first search traversal algorithms in detail, including pseudocode. It analyzes the time complexity of these algorithms. Finally, it briefly discusses other graph topics like strongly connected components and biconnected components.
This document discusses graphs and graph algorithms. It defines what a graph is - a set of vertices connected by edges. It covers different types of graphs like directed/undirected graphs and weighted graphs. It then explains two common graph search algorithms - depth-first search (DFS) and breadth-first search (BFS). DFS explores each path as deep as possible before backtracking while BFS explores all neighbors of a node before moving deeper. Both algorithms run in O(V+E) time where V is vertices and E is edges. BFS always finds the shortest path while DFS is not guaranteed to.
The document discusses graphs and graph algorithms. It begins by defining what a graph is - a collection of vertices connected by edges. It then lists four learning objectives related to representing graphs, traversing graphs, calculating minimum spanning trees, and finding shortest routes. The document goes on to describe different ways of representing graphs through adjacency matrices and lists. It also explains graph traversal algorithms like depth-first search and breadth-first search. Finally, it discusses algorithms for finding minimum spanning trees and shortest paths in weighted graphs.
Algorithms and data Chapter 3 V Graph.pptxzerihunnana
油
This document discusses graphs and graph algorithms. It defines graphs as collections of vertices and edges. It describes different types of graphs like directed, undirected, weighted graphs. It explains graph traversal algorithms like breadth-first search and depth-first search. It also discusses minimum spanning trees and algorithms to find them, like Prim's algorithm and Kruskal's algorithm.
The document discusses graphs and graph algorithms. It defines what a graph is and how they can be represented. It also explains graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS). Additionally, it covers algorithms for finding the shortest path using Dijkstra's algorithm and calculating minimum spanning trees using Kruskal's algorithm.
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...RVSPSOA
油
Principles of statics. Forces and their effects. Types of force systems. Resultant of concurrent and
parallel forces. Lamis theorem. Principle of moments. Varignons theorem. Principle of equilibrium.
Types of supports and reactions-Bending moment and Shear forces-Determination of reactions for
simply supported beams. Relation between bending moment and shear force.
Properties of section Centre of gravity, Moment of Inertia, Section modulus, Radius of gyration
for various structural shapes. Theorem of perpendicular axis. Theorem of parallel axis.
Elastic properties of solids. Concept of stress and strain. Deformation of axially loaded simple bars.
Types of stresses. Concept of axial and volumetric stresses and strains. Elastic constants. Elastic
Modulus. Shear Modulus. Bulk Modulus. Poissons ratio. Relation between elastic constants.
Principal stresses and strain. Numerical and Graphical method. Mohrs diagram.
R.K. Bansal, A Text book on Engineering Mechanics, Lakshmi Publications, Delhi,2008.
R.K. Bansal, A textbook on Strength of Materials, Lakshmi Publications, Delhi 2010.
Paul W. McMullin, 'Jonathan S. Price, Introduction to Structures, Routledge, 2016.
P.C. Punmia, Strength of Materials and Theory of Structures; Vol. I, Lakshmi
Publications, Delhi 2018.
2. S. Ramamrutham, Strength of Materials, Dhanpatrai and Sons, Delhi, 2014.
3. W.A. Nash, Strength of Materials, Schaums Series, McGraw Hill Book Company,1989.
4. R.K. Rajput, Strength of Materials, S.K. Kataria and Sons, New Delhi , 2017.
IDSP is a disease surveillance program in India that aims to strengthen/maintain decentralized laboratory-based IT enabled disease surveillance systems for epidemic prone diseases to monitor disease trends, and to detect and respond to outbreaks in the early phases swiftly.....
Progressive Web Apps (PWA) are web applications that deliver an app-like experience using modern web technologies, offering features like offline functionality, installability, and responsiveness across devices.
This study describe how to write the Research Paper and its related issues. It also presents the major sections of Research Paper and various tools & techniques used for Polishing Research Paper
before final submission.
Finding a Right Journal and Publication Ethics are explain in brief.
Students will research and orally present a Colombian company using a visual tool, in order to develop their communication skills and intercultural understanding through the exploration of identity, innovation, and local culture, in connection with the IB global themes.
How to Configure Add to Cart in Odoo 18 WebsiteCeline George
油
In this slide, well discuss how to configure the Add to Cart functionality in the Odoo 18 Website. This feature enhances the shopping experience by offering three flexible options: Stay on the Product Page, Go to the Cart, or Let the User Decide through a dialog box.
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...Arshad Shaikh
油
Dictyoptera is an order of insects that includes cockroaches and praying mantises. These insects are characterized by their flat, oval-shaped bodies and unique features such as modified forelegs in mantises for predation. They inhabit diverse environments worldwide.
How to Use Owl Slots in Odoo 17 - Odoo 際際滷sCeline George
油
In this slide, we will explore Owl Slots, a powerful feature of the Odoo 17 web framework that allows us to create reusable and customizable user interfaces. We will learn how to define slots in parent components, use them in child components, and leverage their capabilities to build dynamic and flexible UIs.
RELATIONS AND FUNCTIONS
1. Cartesian Product of Sets:
If A and B are two non-empty sets, then their Cartesian product is:
A B = {(a, b) | a A, b B}
Number of elements: |A B| = |A| |B|
2. Relation:
A relation R from set A to B is a subset of A B.
Domain: Set of all first elements.
Range: Set of all second elements.
Codomain: Set B.
3. Types of Relations:
Empty Relation: No element in R.
Universal Relation: R = A A.
Identity Relation: R = {(a, a) | a A}
Reflexive: (a, a) R a A
Symmetric: (a, b) R (b, a) R
Transitive: (a, b), (b, c) R (a, c) R
Equivalence Relation: Reflexive, symmetric, and transitive
4. Function (Mapping):
A relation f: A B is a function if every element of A has exactly one image in B.
Domain: A, Codomain: B, Range B
5. Types of Functions:
One-one (Injective): Different inputs give different outputs.
Onto (Surjective): Every element of codomain is mapped.
One-one Onto (Bijective): Both injective and surjective.
Constant Function: f(x) = c x A
Identity Function: f(x) = x
Polynomial Function: e.g., f(x) = x族 + 1
Modulus Function: f(x) = |x|
Greatest Integer Function: f(x) = [x]
Signum Function: f(x) =
-1 if x < 0,
0 if x = 0,
1 if x > 0
6. Graphs of Functions:
Learn shapes of basic graphs: modulus, identity, step function, etc.
Order: Odonata Isoptera and Thysanoptera.pptxArshad Shaikh
油
*Odonata*: Odonata is an order of insects that includes dragonflies and damselflies. Characterized by their large, compound eyes and agile flight, they are predators that feed on other insects, playing a crucial role in maintaining ecological balance.
*Isoptera*: Isoptera is an order of social insects commonly known as termites. These eusocial creatures live in colonies with complex social hierarchies and are known for their ability to decompose wood and other cellulose-based materials, playing a significant role in ecosystem nutrient cycling.
*Thysanoptera*: Thysanoptera, or thrips, are tiny insects with fringed wings. Many species are pests that feed on plant sap, transmitting plant viruses and causing damage to crops and ornamental plants. Despite their small size, they have significant impacts on agriculture and horticulture.
How to Manage Orders in Odoo 18 Lunch - Odoo 際際滷sCeline George
油
The Lunch module in Odoo 18 helps users place their food orders, making meal management seamless and efficient. It allows employees to browse available options, place orders, and track their meals effortlessly.
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdfChalaKelbessa
油
This is Forestry Exit Exam Model for 2025 from Department of Forestry at Wollega University, Gimbi Campus.
The exam contains forestry courses such as Dendrology, Forest Seed and Nursery Establishment, Plantation Establishment and Management, Silviculture, Forest Mensuration, Forest Biometry, Agroforestry, Biodiversity Conservation, Forest Business, Forest Fore, Forest Protection, Forest Management, Wood Processing and others that are related to Forestry.
"Orthoptera: Grasshoppers, Crickets, and Katydids pptxArshad Shaikh
油
Orthoptera is an order of insects that includes grasshoppers, crickets, and katydids. Characterized by their powerful hind legs, Orthoptera are known for their impressive jumping ability. With diverse species, they inhabit various environments, playing important roles in ecosystems as herbivores and prey. Their sounds, often produced through stridulation, are distinctive features of many species.
"Hymenoptera: A Diverse and Fascinating Order".pptxArshad Shaikh
油
Hymenoptera is a diverse order of insects that includes bees, wasps, ants, and sawflies. Characterized by their narrow waists and often social behavior, Hymenoptera play crucial roles in ecosystems as pollinators, predators, and decomposers, with many species exhibiting complex social structures and communication systems.
Order Lepidoptera: Butterflies and Moths.pptxArshad Shaikh
油
Lepidoptera is an order of insects comprising butterflies and moths. Characterized by scaly wings and a distinct life cycle, Lepidoptera undergo metamorphosis from egg to larva (caterpillar) to pupa (chrysalis or cocoon) and finally to adult. With over 180,000 described species, they exhibit incredible diversity in form, behavior, and habitat, playing vital roles in ecosystems as pollinators, herbivores, and prey. Their striking colors, patterns, and adaptations make them a fascinating group for study and appreciation.
Here is the current update:
CURRENT CASE COUNT: 897
- Texas: 742 (+14) (55% of cases are in Gaines County). Includes additional numbers from El Paso.
- New Mexico: 79 (+1) (83% of cases are from Lea County)
- Oklahoma: 17
- Kansas: 59 (+3) (38.89% of the cases are from Gray County)
HOSPITALIZATIONS: 103
- Texas: 94 This accounts for 13% of all cases in Texas.
- New Mexico: 7 This accounts for 9.47% of all cases in New Mexico.
- Kansas: 3 This accounts for 5.08% of all cases in Kansas.
DEATHS: 3
- Texas: 2 This is 0.28% of all cases in Texas.
- New Mexico: 1 This is 1.35% of all cases in New Mexico.
US NATIONAL CASE COUNT: 1,132 (confirmed and suspected)
INTERNATIONAL SPREAD
Mexico: 1,856(+103), 4 fatalities
- Chihuahua, Mexico: 1,740 (+83) cases, 3 fatalities, 4 currently hospitalized.
Canada: 2,791 (+273)
- Ontario, Canada: 1,938 (+143) cases. 158 (+29) hospitalizations
- Alberta, Canada: 679 (+119) cases. 4 currently hospitalized
2. 1. Graph Traversal
Definition:
A graph traversal means visiting all the vertices (nodes) of the
graph.
- Traversal is the facility to move through a structure visiting each of
the vertices once.
Two traversal methods for a graph are breadth-first and depth-first.
3. 2. Breadth-First Graph Traversal
Breadth-first traversal makes use of a queue data structure. The
queue holds a list of vertices which have not been visited yet but
which should be visited soon.
Since a queue is a first-in first-out structure, vertices are visited in
the order in which they are added to the queue.
Visiting a vertex involves, for example, outputting the data stored
in that vertex, and also adding its neighbours to the queue.
Neighbours are not added to the queue if they are already in the
queue, or have already been visited.
In BFS;
- One node is selected as a start position. Its visited and marked.
- Then, all unvisited nodes adjacent of the next node are visited and
marked in some sequential order.
4. 2. Breadth-First Search [Example-1] (1/5)
Step-1:
Step-2:
4
A
B
S
C
D
E
F
G H
Queue Status
Output: A
B
S
A
B
S
C
D
E
F
G H
Output: A, B,
Queue Status
S
5. 2. Breadth-First Search [Example-1] (2/5)
Step-3:
Step-4:
5
A
B
S
C
D
E
F
G H
Queue Status
Output: A, B, S,
C
G
A
B
S
C
D
E
F
G H
Output: A, B, S, C,
Queue Status
G
D
E
F
6. 2. Breadth-First Search [Example-1] (3/5)
Step-5:
Step-6:
6
A
B
S
C
D
E
F
G H
Queue Status
Output: A, B, S, C, G,
D
E
F
H
A
B
S
C
D
E
F
G H
Output: A, B, S, C, G, D,
Queue Status
E
F
H
7. 2. Breadth-First Search [Example-1] (4/5)
Step-7:
Step-8:
7
A
B
S
C
D
E
F
G H
Queue Status
Output: A, B, S, C, G, D, E,
F
H
A
B
S
C
D
E
F
G H
Output: A, B, S, C, G, D, E, F
Queue Status
H
8. 2. Breadth-First Search [Example-1] (5/5)
Step-9:
8
A
B
S
C
D
E
F
G H
Queue Status
Output: A, B, S, C, G, D, E, F, H [Hence Proved]
9. If we start at vertex 1 then a valid breadth-first order
would be:
9
2
5
3
0
6
4
7
1
1,2,3,7,6,0,5,4
2. Breadth-First Search [Example-2]
10. 10
Breadth First Search:
B
C
D
E
F
H
G
A
I
A B D E
A B D E F
A B D E F C H
A B D E F C H G I
As was true with depth-first search, a breadth-first search from some vertices
may fail to locate all the vertices.
A breadth-first search from B:
B F G I H C D
2. Breadth-First Search [Example-3]
11. A
H
B
F
E
D
C
G
A
B
C
D
E
F
G
H
How is this accomplished? Simply replace the stack with a queue!
Rules: (1) Maintain an enqueued array. (2) Visit node when
dequeued.
Q
2. Breadth-First Search [Example-4] (1/11)
13. A
H
B
F
E
D
C
G
A
B
C
D
E
F
G
H
Dequeue D. Visit D. Enqueue unenqueued nodes adjacent to D.
Q C E F
Nodes visited: D
2. Breadth-First Search [Example-4] (3/11)
14. A
H
B
F
E
D
C
G
A
B
C
D
E
F
G
H
Dequeue C. Visit C. Enqueue unenqueued nodes adjacent to C.
Q E F
Nodes visited: D, C
2. Breadth-First Search [Example-4] (4/11)
15. A
H
B
F
E
D
C
G
A
B
C
D
E
F
G
H
Dequeue E. Visit E. Enqueue unenqueued nodes adjacent to
E.
Q F G
Nodes visited: D, C, E
2. Breadth-First Search [Example-4] (5/11)
16. A
H
B
F
E
D
C
G
A
B
C
D
E
F
G
H
Dequeue F. Visit F. Enqueue unenqueued nodes adjacent to F.
Q G
Nodes visited: D, C, E, F
2. Breadth-First Search [Example-4] (6/11)
17. A
H
B
F
E
D
C
G
A
B
C
D
E
F
G
H
Dequeue G. Visit G. Enqueue unenqueued nodes adjacent to G.
Q H
Nodes visited: D, C, E, F, G
2. Breadth-First Search [Example-4] (7/11)
18. A
H
B
F
E
D
C
G
A
B
C
D
E
F
G
H
Dequeue H. Visit H. Enqueue unenqueued nodes adjacent to H.
Q A B
Nodes visited: D, C, E, F, G, H
2. Breadth-First Search [Example-4] (8/11)
19. A
H
B
F
E
D
C
G
A
B
C
D
E
F
G
H
Dequeue A. Visit A. Enqueue unenqueued nodes adjacent to
A.
Q B
Nodes visited: D, C, E, F, G, H, A
2. Breadth-First Search [Example-4] (9/11)
20. A
H
B
F
E
D
C
G
A
B
C
D
E
F
G
H
Dequeue B. Visit B. Enqueue unenqueued nodes adjacent to B.
Q empty
Nodes visited: D, C, E, F, G, H, A, B
2. Breadth-First Search [Example-4] (10/11)
21. A
H
B
F
E
D
C
G
A
B
C
D
E
F
G
H
Q empty. Algorithm done.
Q empty
Nodes visited: D, C, E, F, G, H, A, B
2. Breadth-First Search [Example-4] (11/11)
22. 1. Visit the start vertex
2. Initialize queue to contain the start vertex
3. While queue not empty do
a. Remove a vertex v from the queue
b. For all vertices w adjacent to v do:
If w has not been visited then:
i. Visit w
ii. Add w to queue
End while
22
2. Breadth-First Search [Algorithm]
23. 3. Depth-First Graph Traversal
23
In DFS;
- Depth first search (DFS) follows first a path from the starting node to an
ending node.
- Then, another path from the start to the End and so forth until all nodes
have been visited.
Data stored in DFS as stack (working as Push and Pop).
Steps of DFGT:
- stack works as LIFO (Last In First Out).
This method visits all the vertices, beginning with a specified start vertex.
This strategy proceeds along a path from vertex V as deeply into the graph as
possible.
This means that after visiting V, the algorithm tries to visit any unvisited
vertex adjacent to V.
When the traversal reaches a vertex which has no adjacent vertex, it back
tracks and visits an unvisited adjacent vertex.
24. 3. Depth-First Search [Example-1] (1/6)
Step-1:
Step-2:
24
A
B
S
C
D
E
F
G H
Stack
Output: A
A
A
B
S
C
D
E
F
G H
Output: A, B,
Stack
B
A
No adjacent. So
B become
Pop
25. 3. Depth-First Search [Example-1] (2/6)
Step-3:
Step-4:
25
A
B
S
C
D
E
F
G H
Stack
Output: A, B, S,
S
A
A
B
S
C
D
E
F
G H
Output: A, B, S, C,
Stack
C
S
A
26. 3. Depth-First Search [Example-1] (3/6)
Step-5:
Step-6:
26
A
B
S
C
D
E
F
G H
Stack
Output: A, B, S, C, D
A
B
S
C
D
E
F
G H
Output: A, B, S, C, D
Stack
D
C
S
A
D
C
S
A
No adjacent. So
D become
Pop
27. 3. Depth-First Search [Example-1] (4/6)
Step-7:
Step-8:
27
A
B
S
C
D
E
F
G H
Stack
Output: A, B, S, C, D, E
A
B
S
C
D
E
F
G H
Output: A, B, S, C, D, E, H,
Stack
H
E
C
S
A
E
C
S
A
28. 3. Depth-First Search [Example-1] (5/6)
Step-9:
Step-10:
28
A
B
S
C
D
E
F
G H
Stack
Output: A, B, S, C, D, E, H, G,
A
B
S
C
D
E
F
G H
Output: A, B, S, C, D, E, H,G, F
Stack
G
H
E
C
S
A
F
G
H
E
C
S
A
29. 3. Depth-First Search [Example-1] (6/6)
Step-10:
29
A
B
S
C
D
E
F
G H
Output: A, B, S, C, D, E, H,G, F
Stack
No adjacent. So
F become
Pop.
No adjacent. So
G become
Pop.
No adjacent. So
H become
Pop.
No adjacent. So
E become
Pop.
No adjacent. So
C become
Pop.
No adjacent. So
S become
Pop.
No adjacent. So
A become
Pop.
30. If we start at vertex 1 then a valid depth-first order
would be:
30
2
5
3
0
6
4
7
1
1, 2, 3, 6, 4, 5, 7, 0
3. Depth-First Search [Example-2]
32. 32
B
C
D
E
F
H
G
A
I
Design Starting depth first search at start vertices as;
(i) Vertices A
(ii) Vertices C
(iii) Vertices G
3. Depth-First Search [Class Participation]
35. 4.1 Shortest Paths
Definition: The shortest path problem is about finding a path
between vertices in a graph such that the total sum of the edges
weights is minimum.
Problem: Given some vertex s, find the shortest path from s to every
other vertex in G.
Suppose we have a weighted graph G:
The cost of traversing edge (i, j) is ci,j .
The cost of traversing a path is the sum of the edge costs.
59
36. Phone/Internet communications: to find the cheapest/fastest path
between two computers.
Transportation/Travelling: to find the cheapest/fastest path between
two cities.
60
4.1 Shortest Paths [Applications]
37. Dijkstras algorithm is an algorithm for finding the shortest paths between
nodes in a graph which may represent.
For example; Road networks.
Steps of Dijkstras Algorithm:
1. Initialize distances according to the algorithm.
2. Pick 1st
node and calculate distances to adjacent nodes.
3. Pick next node with minimal distance; repeat adjacent node distance
calculations.
61
4.2 Shortest Paths [Dijkstras Algorithm]
38. Ex
Step-1: Start with source node (i.e., a). And Initialize it with 0.
Other nodes become infinite ().
62
4.2 Dijkstras Algorithm [Example-1] (1/4)
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
0
39. Step-2: Source node called as u. and destination as v. Find shortest path.
Step-3: Applying formula. c=2.
63
4.2 Dijkstras Algorithm [Example-1] (2/4)
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
0
u
v
Formula:
If (d (u) + c (u, v) < d(v)}
d (v) = d(u) + c (u, v)
If (0 + 2 < )
2 < [True]
d (v) = d(u) + c (u, v)
= 0 + 2 = 2
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
0
2
40. Step-4: Find shortest path from c. u and v changes.
Step-5: Find shortest path from b. u and v changes.
64
4.2 Dijkstras Algorithm [Example-1] (3/4)
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
0
3
2
u
v
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
0
3
2
u
8 v
41. Step-6: Find shortest path from d. u and v changes.
Step-7: Find shortest path from e. u and v changes.
65
4.2 Dijkstras Algorithm [Example-1] (4/4)
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
0
3
2
u
8
v
10
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
0
3
2
15
10
Marked (red line) is
most shortest path to
reach destination.
Total cost 15.
42. 4.2 Dijkstras Algorithm [Example-2] (1/15)
Find the shortest path from vertex A to every
other vertex?
Step-1: Create Shortest distance from A and Previous vertex columns.
62. 95
5.1 Minimum Spanning Tree (MST)
it is a tree (i.e., it is acyclic)
it covers all the vertices V
contains |V| - 1 edges
the total cost associated with tree edges is the
minimum among all possible spanning trees
not necessarily unique
A minimum spanning tree is a subgraph of an
undirected weighted graph G, such that
63. 96
Prim's algorithm is a famous greedy algorithm.
- It is used for finding the Minimum Spanning Tree (MST) of a
given graph.
To apply Prims algorithm,
- the given graph must be weighted, connected and undirected.
Steps of Prims Algorithm:
1. Remove all loop edges.
2. Remove all parallel edges.
3. Choose any arbitrary node as root node.
5.2 Minimum Spanning Tree (Prims Algorithm)
64. 97
5.2 Minimum Spanning Tree (Prims Algorithm)
[Example-1]
B
A
C
D
E
F
7
8
3
6
4
3
8
8
2
5
2
Step-1: Remove all loop edges
B
A
C
D
E
F
7
8
3
6
4
3
8
2
5
2
Step-2: Remove all Parallel edges.
If more than edges between 2 nodes;
remove high weighted nodes.
B
A
C
D
E
F
7
8
3
6
4
3
2
5
2
65. 98
5.2 Minimum Spanning Tree (Prims Algorithm)
[Example-1]
Step-3: Choose any arbitrary node as root node. We selected A vertices.
B
A
C
D
E
F
7
8
3
6
4
3
8
2
5
2
A
Step-4: Check outgoing edges (A) and select the one with less cost.
B
A
7 Remaining edges: AC(8)
Step-5: Check outgoing edges (B) and select the one with less cost.
B
A
7
C
3
Selected edges: AC(8), BD(6), BC(3)
Remaining edges: AC(8), BD(6)
Selected edges: AC(8), AB(7)
66. 99
5.2 Minimum Spanning Tree (Prims Algorithm)
[Example-1]
Step-6: Check outgoing edges (C) and select the one with less cost.
B
A
7
C
3
Selected edges: AC(8), BD(6), CD(4), CE(3)
Remaining edges: AC(8), BD(6), CD(4),
E
3
Step-7: Check outgoing edges (C) and select the one with less cost.
B
A
7
C
3
E
3
Selected edges: AC(8), BD(6), CD(4), ED(2), EF(2)
Remaining edges: AC(8), BD(6), CD(4), EF(2)
Select anyone.
Both are minimum
values.
D
2
67. 100
5.2 Minimum Spanning Tree (Prims Algorithm)
[Example-1]
Step-7: Check outgoing edges (C) and select the one with less cost.
Selected edges: AC(8), BD(6), CD(4), EF(2), DF(5)
Remaining edges: AC(8), BD(6), CD(4), DF(5)
EF(2) is selected
because of
minimum value in
queue..
B
A
7
C
3
E
3
D
2 F
2
Total vertices (V)= 6, Edge (E) = | V | - 1
= 6 1 = 5 [Hence Proof, Minimum Spanning Tree]
Final conclusion
68. Kruskals Algorithm
101
Initialization
a. Create a forest of vertices F
b. Initialize the set of safe edges T
comprising the MST to the empty set
c. Sort edges by increasing weight
a
c
e
d
b
2
4
5
9
6
4
5
5
F = {a}, {b}, {c}, {d}, {e}
T =
E = {(a,d), (c,d), (d,e), (a,c),
(b,e), (c,e), (b,d), (a,b)}