The document discusses Breadth First Search (BFS) and Depth First Search (DFS) algorithms for graphs. It defines key graph terminology and describes how to represent graphs using adjacency lists and matrices. It then explains how BFS and DFS work by exploring the graph in different ways, keeping track of vertex colors and paths/trees produced. Both algorithms run in O(V+E) time and have different applications like routing, sorting, and finding connected components.
This document summarizes a lab on data structures and algorithms related to graphs. It introduces graphs and their representations, as well as graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS). It also discusses bipartite graphs. The lab objectives are to learn about graphs, DFS, BFS, and bipartite graphs. Various exercises are provided to apply these graph concepts.
Breadth-first search (BFS) is an algorithm for traversing or searching trees and graphs. It starts at a root node and explores all neighboring nodes at the present depth prior to moving on to the nodes at the next depth level. BFS runs in O(n+m) time on graphs represented with adjacency lists, where it can be used to find connected components, compute spanning forests, and solve other graph problems by labeling edges and vertices during the traversal.
This document provides an overview of the Breadth-First Search (BFS) graph traversal algorithm. It begins by explaining that BFS starts at the root node and explores all neighboring nodes, selecting the nearest unexplored node at each step. It then provides examples of BFS traversing different graphs and describes the key aspects of the BFS algorithm, including maintaining a queue, coloring nodes as white/gray/black to track explored status, and updating distance and predecessor values at each step. In the end, it analyzes the properties of BFS, such as finding the shortest path distances, and discusses applications like finding connected components.
Elementary Graph Algorithms discusses graphs and common graph algorithms. It defines graphs as G = (V, E) with vertices V and edges E. It describes the breadth-first search (BFS) and depth-first search (DFS) algorithms. BFS expands the frontier between discovered and undiscovered vertices uniformly across the breadth of the frontier. It uses a queue and runs in O(V+E) time. DFS explores edges out of the most recently discovered vertex, searching as deep as possible first before backtracking. It uses timestamps and runs in O(V+E) time. Pseudocode and examples are provided for both algorithms.
The document discusses depth-first search (DFS) algorithms. It provides pseudocode for performing DFS on a graph and walks through an example of running DFS on a sample graph. It explains that DFS classifies edges in the original graph as tree edges, back edges, forward edges, or cross edges based on how vertices are encountered during the algorithm's execution.
BFS is the most commonly used approach. BFS is a traversing algorithm where you should start traversing from a selected node (source or starting node) and traverse the graph layerwise thus exploring the neighbor nodes (nodes which are directly connected to the source node.
The document discusses graph algorithms breadth-first search (BFS) and depth-first search (DFS). It provides pseudocode for BFS and DFS algorithms and explains key aspects of each. BFS uses a queue to explore all neighbors of a node before moving to the next level, building a breadth-first tree. DFS uses recursion to explore as deep as possible along each branch before backtracking, resulting in a depth-first tree structure. Both algorithms run in O(V+E) time on a graph with V vertices and E edges.
The document discusses Breadth First Search (BFS) and Depth First Search (DFS) algorithms for graphs. It defines key graph terminology and describes how to represent graphs using adjacency lists and matrices. It then explains how BFS and DFS work by exploring the graph in different ways, keeping track of vertex colors and paths/trees produced. Both algorithms run in O(V+E) time and have different applications like routing, sorting, and finding connected components.
This document summarizes a lab on data structures and algorithms related to graphs. It introduces graphs and their representations, as well as graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS). It also discusses bipartite graphs. The lab objectives are to learn about graphs, DFS, BFS, and bipartite graphs. Various exercises are provided to apply these graph concepts.
Breadth-first search (BFS) is an algorithm for traversing or searching trees and graphs. It starts at a root node and explores all neighboring nodes at the present depth prior to moving on to the nodes at the next depth level. BFS runs in O(n+m) time on graphs represented with adjacency lists, where it can be used to find connected components, compute spanning forests, and solve other graph problems by labeling edges and vertices during the traversal.
This document provides an overview of the Breadth-First Search (BFS) graph traversal algorithm. It begins by explaining that BFS starts at the root node and explores all neighboring nodes, selecting the nearest unexplored node at each step. It then provides examples of BFS traversing different graphs and describes the key aspects of the BFS algorithm, including maintaining a queue, coloring nodes as white/gray/black to track explored status, and updating distance and predecessor values at each step. In the end, it analyzes the properties of BFS, such as finding the shortest path distances, and discusses applications like finding connected components.
Elementary Graph Algorithms discusses graphs and common graph algorithms. It defines graphs as G = (V, E) with vertices V and edges E. It describes the breadth-first search (BFS) and depth-first search (DFS) algorithms. BFS expands the frontier between discovered and undiscovered vertices uniformly across the breadth of the frontier. It uses a queue and runs in O(V+E) time. DFS explores edges out of the most recently discovered vertex, searching as deep as possible first before backtracking. It uses timestamps and runs in O(V+E) time. Pseudocode and examples are provided for both algorithms.
The document discusses depth-first search (DFS) algorithms. It provides pseudocode for performing DFS on a graph and walks through an example of running DFS on a sample graph. It explains that DFS classifies edges in the original graph as tree edges, back edges, forward edges, or cross edges based on how vertices are encountered during the algorithm's execution.
BFS is the most commonly used approach. BFS is a traversing algorithm where you should start traversing from a selected node (source or starting node) and traverse the graph layerwise thus exploring the neighbor nodes (nodes which are directly connected to the source node.
The document discusses graph algorithms breadth-first search (BFS) and depth-first search (DFS). It provides pseudocode for BFS and DFS algorithms and explains key aspects of each. BFS uses a queue to explore all neighbors of a node before moving to the next level, building a breadth-first tree. DFS uses recursion to explore as deep as possible along each branch before backtracking, resulting in a depth-first tree structure. Both algorithms run in O(V+E) time on a graph with V vertices and E edges.
The document introduces breadth-first search (BFS) and depth-first search (DFS), two algorithms for traversing graphs. It provides explanations of how BFS and DFS work, including pseudocode algorithms. Examples of simulations of BFS and DFS on sample graphs are shown step-by-step with diagrams. Potential applications of BFS and DFS in areas like cycle detection, finding connected components, and topological sorting are also discussed.
This document describes graph search algorithms like breadth-first search (BFS) and their applications. It provides details on how BFS works, including that it maintains queues to search levels outwards from the starting vertex, and outputs the distance and predecessor of each vertex. BFS runs in O(V+E) time by visiting each vertex and edge once. The document also discusses how BFS can be used to find connected components in a graph and determine if a graph is bipartite.
This document describes breadth-first search (BFS) graph algorithms. It begins by defining graphs and different graph representations. It then explains BFS, which explores a graph by exploring all of the neighbor nodes of the starting node, then all of the neighbors of the neighbors, and so on outwards in "waves" or breadth-first manner. The document provides pseudocode for BFS and walks through an example run on a sample graph to demonstrate how BFS works. It concludes by discussing the running time of BFS and providing an example programming assignment for implementing BFS.
The document discusses Depth First Search (DFS) graph traversal algorithms. It explains that DFS traverses the graph by going as deep as possible along each branch before backtracking. It provides examples of DFS on graphs, showing the order of vertex visits and maintaining discovery/finish times. Key aspects of DFS include using recursion to visit nodes, marking nodes gray while discovering and black when finished, and distinguishing tree, back, forward and cross edges. The time complexity of DFS is O(V+E) where V is vertices and E is edges. Applications mentioned are topological sorting and finding strongly connected components.
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.
Breadth-first search (BFS) and depth-first search (DFS) are two graph traversal algorithms that systematically visit all vertices and edges of a graph. BFS discovers vertices in order of distance from the source vertex, allowing it to compute shortest paths. DFS recursively explores as far as possible along each branch before backtracking, imposing a tree structure on the graph. Both algorithms run in O(V+E) time, where V is the number of vertices and E is the number of edges. DFS classifies edges as tree, forward, back, or cross edges and can detect cycles based on the presence of back edges.
Skiena algorithm 2007 lecture11 breadth deapth first searchzukun
This document discusses breadth-first search (BFS), an algorithm for traversing graphs in a level-by-level fashion starting from a root node. It describes how BFS works by marking vertices as undiscovered, discovered, or processed and maintaining a queue of discovered vertices. BFS is used to find shortest paths in unweighted graphs by discovering vertices in order of distance from the root. The document also discusses applications of BFS like finding connected components and testing if a graph can be two-colored.
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.
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.
Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking
This document discusses depth-first search (DFS) algorithms and their applications to finding spanning trees and articulation points in graphs. It begins by explaining how DFS generalizes preorder tree traversal and avoids cycles in arbitrary graphs by marking visited vertices. DFS takes O(V+E) time on graphs. The document then explains how DFS can be used to find a depth-first spanning tree and identify biconnected components and articulation points via numbering schemes like num(v) and low(v).
This document contains a presentation on Breadth-First Search (BFS) given to students. The presentation includes:
- An introduction to BFS and its inventor Konrad Zuse.
- Definitions of key terms like graph, tree, vertex, level-order traversal.
- An example visualization of BFS on a graph with 14 steps.
- Pseudocode and a Java program implementing BFS.
- Applications of BFS like shortest paths, social networks, web crawlers.
- The time and space complexity of BFS is O(V+E) and O(V).
- A conclusion that BFS is an important algorithm that traverses
Depth-first search (DFS) is an algorithm that explores all the vertices reachable from a starting vertex by traversing edges in a depth-first manner. DFS uses a stack data structure to keep track of vertices to visit. It colors vertices white, gray, and black to indicate their status. DFS runs in O(V+E) time and can be used for applications like topological sorting and finding strongly connected components. The edges discovered during DFS can be classified as tree, back, forward, or cross edges based on the order in which vertices are discovered.
The document discusses different types of graphs including directed and undirected graphs. It describes graph implementations using adjacency matrices and adjacency lists. It also covers graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS). DFS prioritizes exploring paths as deeply as possible while BFS explores all paths at the same depth level before moving deeper. Real-world examples of graphs include social networks and transportation networks.
This document provides an overview of basic graph algorithms. It begins with examples of graphs in everyday life and a brief history of graph theory starting with Euler. It then covers basic graph terminology and properties like nodes, edges, degrees. Common representations of graphs in computers like adjacency lists and matrices are described. Breadth-first search and depth-first search algorithms for traversing graphs are introduced. Finally, applications of graph algorithms like finding paths, connected components, and topological sorting are mentioned.
This document defines key graph terminology and concepts. It begins by defining what a graph is composed of - vertices and edges. It then discusses directed vs undirected graphs and defines common graph terms like adjacent vertices, paths, cycles, and more. The document also covers different ways to represent graphs, such as adjacency matrices and adjacency lists. Finally, it briefly introduces common graph search methods like breadth-first search and depth-first search.
The document discusses graphs and their representations. It defines graphs as sets of nodes connected by edges. It describes two common representations of graphs - adjacency lists and adjacency matrices. It provides examples and analyzes the time complexity of operations for each representation. It also discusses directed and undirected graphs, paths, cycles, and other graph terminology. Finally, it poses problems about computing graph properties and determining if a graph contains a universal sink.
The document introduces breadth-first search (BFS) and depth-first search (DFS), two algorithms for traversing graphs. It provides explanations of how BFS and DFS work, including pseudocode algorithms. Examples of simulations of BFS and DFS on sample graphs are shown step-by-step with diagrams. Potential applications of BFS and DFS in areas like cycle detection, finding connected components, and topological sorting are also discussed.
This document describes graph search algorithms like breadth-first search (BFS) and their applications. It provides details on how BFS works, including that it maintains queues to search levels outwards from the starting vertex, and outputs the distance and predecessor of each vertex. BFS runs in O(V+E) time by visiting each vertex and edge once. The document also discusses how BFS can be used to find connected components in a graph and determine if a graph is bipartite.
This document describes breadth-first search (BFS) graph algorithms. It begins by defining graphs and different graph representations. It then explains BFS, which explores a graph by exploring all of the neighbor nodes of the starting node, then all of the neighbors of the neighbors, and so on outwards in "waves" or breadth-first manner. The document provides pseudocode for BFS and walks through an example run on a sample graph to demonstrate how BFS works. It concludes by discussing the running time of BFS and providing an example programming assignment for implementing BFS.
The document discusses Depth First Search (DFS) graph traversal algorithms. It explains that DFS traverses the graph by going as deep as possible along each branch before backtracking. It provides examples of DFS on graphs, showing the order of vertex visits and maintaining discovery/finish times. Key aspects of DFS include using recursion to visit nodes, marking nodes gray while discovering and black when finished, and distinguishing tree, back, forward and cross edges. The time complexity of DFS is O(V+E) where V is vertices and E is edges. Applications mentioned are topological sorting and finding strongly connected components.
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.
Breadth-first search (BFS) and depth-first search (DFS) are two graph traversal algorithms that systematically visit all vertices and edges of a graph. BFS discovers vertices in order of distance from the source vertex, allowing it to compute shortest paths. DFS recursively explores as far as possible along each branch before backtracking, imposing a tree structure on the graph. Both algorithms run in O(V+E) time, where V is the number of vertices and E is the number of edges. DFS classifies edges as tree, forward, back, or cross edges and can detect cycles based on the presence of back edges.
Skiena algorithm 2007 lecture11 breadth deapth first searchzukun
This document discusses breadth-first search (BFS), an algorithm for traversing graphs in a level-by-level fashion starting from a root node. It describes how BFS works by marking vertices as undiscovered, discovered, or processed and maintaining a queue of discovered vertices. BFS is used to find shortest paths in unweighted graphs by discovering vertices in order of distance from the root. The document also discusses applications of BFS like finding connected components and testing if a graph can be two-colored.
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.
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.
Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking
This document discusses depth-first search (DFS) algorithms and their applications to finding spanning trees and articulation points in graphs. It begins by explaining how DFS generalizes preorder tree traversal and avoids cycles in arbitrary graphs by marking visited vertices. DFS takes O(V+E) time on graphs. The document then explains how DFS can be used to find a depth-first spanning tree and identify biconnected components and articulation points via numbering schemes like num(v) and low(v).
This document contains a presentation on Breadth-First Search (BFS) given to students. The presentation includes:
- An introduction to BFS and its inventor Konrad Zuse.
- Definitions of key terms like graph, tree, vertex, level-order traversal.
- An example visualization of BFS on a graph with 14 steps.
- Pseudocode and a Java program implementing BFS.
- Applications of BFS like shortest paths, social networks, web crawlers.
- The time and space complexity of BFS is O(V+E) and O(V).
- A conclusion that BFS is an important algorithm that traverses
Depth-first search (DFS) is an algorithm that explores all the vertices reachable from a starting vertex by traversing edges in a depth-first manner. DFS uses a stack data structure to keep track of vertices to visit. It colors vertices white, gray, and black to indicate their status. DFS runs in O(V+E) time and can be used for applications like topological sorting and finding strongly connected components. The edges discovered during DFS can be classified as tree, back, forward, or cross edges based on the order in which vertices are discovered.
The document discusses different types of graphs including directed and undirected graphs. It describes graph implementations using adjacency matrices and adjacency lists. It also covers graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS). DFS prioritizes exploring paths as deeply as possible while BFS explores all paths at the same depth level before moving deeper. Real-world examples of graphs include social networks and transportation networks.
This document provides an overview of basic graph algorithms. It begins with examples of graphs in everyday life and a brief history of graph theory starting with Euler. It then covers basic graph terminology and properties like nodes, edges, degrees. Common representations of graphs in computers like adjacency lists and matrices are described. Breadth-first search and depth-first search algorithms for traversing graphs are introduced. Finally, applications of graph algorithms like finding paths, connected components, and topological sorting are mentioned.
This document defines key graph terminology and concepts. It begins by defining what a graph is composed of - vertices and edges. It then discusses directed vs undirected graphs and defines common graph terms like adjacent vertices, paths, cycles, and more. The document also covers different ways to represent graphs, such as adjacency matrices and adjacency lists. Finally, it briefly introduces common graph search methods like breadth-first search and depth-first search.
The document discusses graphs and their representations. It defines graphs as sets of nodes connected by edges. It describes two common representations of graphs - adjacency lists and adjacency matrices. It provides examples and analyzes the time complexity of operations for each representation. It also discusses directed and undirected graphs, paths, cycles, and other graph terminology. Finally, it poses problems about computing graph properties and determining if a graph contains a universal sink.
This document discusses graphs and Eulerian paths and circuits. It begins by defining what a graph is - a set of nodes and edges. It then describes different types of graphs like directed/undirected, cyclic/acyclic, labeled, and weighted graphs. It explains the concepts of adjacency and connectivity in graphs. The document focuses on Eulerian paths and circuits, providing properties for a graph to have an Eulerian path or circuit. It presents pseudocode for an algorithm to check if a graph is Eulerian using depth-first search. Finally, it includes C++ code for a program that tests if input graphs exhibit an Eulerian path or circuit.
Graph Representation, DFS and BFS Presentation.pptxbashirabdullah789
The document describes the breadth-first search (BFS) algorithm for traversing graphs. BFS uses a queue to visit all neighboring nodes at the current level before moving to the next level out. It marks nodes as discovered, processes their neighbors, and marks them finished after processing. The algorithm initializes all nodes as undiscovered, enqueues the source node, and dequeues nodes to process their neighbors until the queue is empty.
Graph theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects. A graph consists of vertices connected by edges. The magnitude of a graph is characterized by the number of vertices and edges. Directed graphs have edges represented as ordered pairs of vertices, while undirected graphs have edges represented as unordered vertex pairs. The degree of a vertex is the number of edges connected to it. For directed graphs, in-degree is the number of edges entering a vertex and out-degree is the number of edges leaving it. The Handshaking Theorem states that for an undirected graph, the sum of the degrees of all vertices equals twice the number of edges.
Graphs can be represented abstractly as a set of vertices and edges connecting pairs of vertices. They are used to model various real-world relationships and can be represented using adjacency lists or matrices. Breadth-first search (BFS) and depth-first search (DFS) are common graph traversal algorithms that systematically explore the edges of a graph to discover reachable vertices. BFS produces a breadth-first tree and finds the shortest paths from the source, while DFS explores edges deeper first before backtracking.
CS-102 Data Structure lectures on Graphsssuser034ce1
The document defines and explains various graph concepts:
- It describes graph representations like adjacency matrices and lists, and types of graphs like undirected, directed, and weighted.
- Key graph terminology is introduced such as vertices, edges, paths, cycles, connectedness, subgraphs, and degrees of vertices.
- Examples are provided to illustrate concepts like complete graphs, trees, and bipartite graphs.
- Graph representations like adjacency matrices and linked/packed adjacency lists are also summarized.
CS-102 Data Structure lectures on Graphsssuser034ce1
The document defines and explains various graph concepts:
- It describes graph representations like adjacency matrices and lists, and types of graphs like undirected, directed, and weighted.
- Key graph terminology is introduced such as vertices, edges, paths, cycles, connectedness, subgraphs, and degrees of vertices.
- Examples are provided to illustrate concepts like complete graphs, trees, and bipartite graphs.
- Graph representations like adjacency matrices and linked/packed adjacency lists are also summarized.
The document discusses various graph theory concepts including:
- Types of graphs such as simple graphs, multigraphs, pseudographs, directed graphs, and directed multigraphs which differ based on allowed edge connections.
- Graph terminology including vertices, edges, degrees, adjacency, incidence, paths, cycles, and representations using adjacency lists and matrices.
- Weighted graphs and algorithms for finding shortest paths such as Dijkstra's algorithm.
- Euler and Hamilton paths/circuits and conditions for their existence.
- The traveling salesman problem of finding the shortest circuit visiting all vertices.
This document provides an overview of graph algorithms and graph theory concepts. It defines different types of graphs including simple graphs, directed graphs, multi graphs, pseudo graphs, and weighted graphs. It also defines graph concepts such as paths, circuits, cycles, degrees of vertices, adjacency matrices, incidence matrices, and graph traversals. Finally, it provides an explanation of the depth-first search algorithm for traversing graphs.
The document provides an introduction to graph theory. It begins with a brief history, noting that graph theory originated from Euler's work on the Konigsberg bridges problem in 1735. It then discusses basic concepts such as graphs being collections of nodes and edges, different types of graphs like directed and undirected graphs. Key graph theory terms are defined such as vertices, edges, degree, walks, paths, cycles and shortest paths. Different graph representations like the adjacency matrix, incidence matrix and adjacency lists are also introduced. The document is intended to provide an overview of fundamental graph theory concepts.
The document discusses graphs and graph theory. It defines graphs as non-linear data structures used to model networks and relationships. The key types of graphs are undirected graphs, where edges have no orientation, and directed graphs, where edges have orientation. Graph traversal algorithms like depth-first search and breadth-first search are discussed. Common graph terminology is defined, including vertices, edges, paths, cycles, degrees. Different graph representations like adjacency matrices and adjacency lists are also covered. Applications of graphs include modeling networks, routes, and relationships.
topologicalsort-using c++ as development language.pptxjanafridi251
topological sort using c++ as programming language to search through a tree structureknljhcffxgchjkjhlkjkfhdffxgchvjbknlkjhgchvjbkjxfghjhiyuighjbyfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffvgvvvvvvvvnbbbbbbbbbfuyfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
The document discusses graph algorithms and graph theory concepts. It defines graphs, directed and undirected graphs, and graph terminology like vertices, edges, adjacency, paths, cycles, connectedness, and representations using adjacency lists and matrices. It also covers tree graphs and their properties.
This document discusses topological sorting and strongly connected components in directed graphs. It defines topological sorting as a linear ordering of the vertices in a directed acyclic graph such that for every edge from vertex u to v, u comes before v in the ordering. It presents Kahn's algorithm for performing topological sorting in O(V+E) time. It also defines strongly connected components as maximal sets of vertices such that there is a path between every pair, and presents an algorithm to find the strongly connected components in a graph in O(V+E) time using depth-first search on the transpose graph.
1. A graph is a collection of objects called vertices that are connected by links called edges. Graphs can be represented as a pair of sets (V,E) where V is the set of vertices and E is the set of edges.
2. There are several important terms used to describe graphs including adjacent nodes, degree of a node, regular graphs, paths, cycles, connected graphs, and complete graphs. Graphs can be represented using adjacency matrices or adjacency lists.
3. There are two main techniques for traversing graphs - depth-first search (DFS) and breadth-first search (BFS). DFS uses a stack and traverses graphs in a depth-wise manner while BFS uses a
Graph theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects. A graph consists of vertices and edges connecting pairs of vertices. There are many types of graphs including trees, which are connected acyclic graphs. Spanning trees are subgraphs of a graph that connect all vertices using the minimum number of edges. Key concepts in graph theory include paths, connectedness, cycles, and isomorphism between graphs.
The document discusses graphs and their basic concepts. It defines what a graph is - an abstract way to represent connectivity using nodes and edges. It explains how graphs are stored using adjacency matrices and adjacency lists. It also defines common graph terminology like vertices, edges, walks, paths etc. Additionally, it discusses special types of graphs like trees and implicit graphs.
This document provides an introduction to graph theory. It defines key graph terminology like vertices, edges, directed and undirected graphs, paths, and connectivity. Examples are given to illustrate different graph types like trees, cycles, and complete graphs. Common graph representations like adjacency matrices and lists are also described. The document outlines theorems and properties of graphs, and discusses applications of graph theory concepts in areas like computer networks and chemistry.
Similar to Depth First Search and Breadth First Search (20)
The document discusses using self-balancing binary search trees like 2-3-4 trees, red-black trees, or AVL trees as an alternative to hashing-based solutions for dynamic dictionaries. These tree-based structures support common dictionary operations like add, lookup, and delete in logarithmic time, as well as predecessor and successor operations that are difficult for hashing approaches. The trees maintain balance, allowing logarithmic-time performance for updates and searches.
This document discusses Bloom filters, a space-efficient randomized data structure for representing a set. Bloom filters support two operations - INSERT, which inserts a key into the set, and MEMBER, which checks if a key is in the set. While MEMBER always returns true for keys in the set, it may occasionally return true for keys not in the set. Bloom filters are useful when the universe of possible keys is very large and operations need to be fast and space-efficient. The document provides examples of how Bloom filters can be used to represent a blacklist of URLs.
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...EduSkills OECD
Andreas Schleicher, Director of Education and Skills at the OECD presents at the launch of PISA 2022 Volume III - Creative Minds, Creative Schools on 18 June 2024.
How to Manage Reception Report in Odoo 17Celine George
A business may deal with both sales and purchases occasionally. They buy things from vendors and then sell them to their customers. Such dealings can be confusing at times. Because multiple clients may inquire about the same product at the same time, after purchasing those products, customers must be assigned to them. Odoo has a tool called Reception Report that can be used to complete this assignment. By enabling this, a reception report comes automatically after confirming a receipt, from which we can assign products to orders.
How to Download & Install Module From the Odoo App Store in Odoo 17Celine George
Custom modules offer the flexibility to extend Odoo's capabilities, address unique requirements, and optimize workflows to align seamlessly with your organization's processes. By leveraging custom modules, businesses can unlock greater efficiency, productivity, and innovation, empowering them to stay competitive in today's dynamic market landscape. In this tutorial, we'll guide you step by step on how to easily download and install modules from the Odoo App Store.
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapitolTechU
Slides from a Capitol Technology University webinar held June 20, 2024. The webinar featured Dr. Donovan Wright, presenting on the Department of Defense Digital Transformation.
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...TechSoup
Whether you're new to SEO or looking to refine your existing strategies, this webinar will provide you with actionable insights and practical tips to elevate your nonprofit's online presence.
1. Data Structures and Algorithms – COMS21103
Representing and Exploring Graphs
Depth First Search and Breadth First Search
Benjamin Sach
2. Graph notation recap
G is a Graph,
Vertex
Edge
V is the set of vertices
E is the set of edges
|V | is the number of vertices
|E| is the number of edges
This lecture focuses on exploring undirected and unweighted graphs
1
3
5
2
4
6
8
7
9
v ∈ V is a vertex
(u, v) ∈ E is an edge from u to v
though everything discussed today will work for directed, unweighted graphs too
3. Graph notation recap
an undirected and unweighted graph an directed and unweighted graph
a directed and weighted graphan undirected and weighted graph
374
9
374
2
83
4. Graph notation recap
an undirected and unweighted graph an directed and unweighted graph
a directed and weighted graphan undirected and weighted graph
374
9
374
2
83
Today we’re
focused on
these graphs
5. Graph notation recap
an undirected and unweighted graph an directed and unweighted graph
a directed and weighted graphan undirected and weighted graph
374
9
374
2
83
Today we’re
focused on
these graphs
But it all works for
these graphs too
6. Graph representations
7
1
3
5
2
4
6
9
0 1 1 0 0 0 0 0 0
1 0 1 1 1 0 0 0 0
1 1 0 1 1 0 0 0 0
0 1 1 0 1 1 0 0 0
0 1 1 1 0 1 0 0 0
0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 1 0 1
0 0 0 0 0 0 1 1 0
1
2
3
4
5
6
7
8
9
1 2 3 4 5 6 7 8 9
FROM
TO
8
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
Adjacency Matrix
|V |
|V |
We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |
7. Graph representations
7
1
3
5
2
4
6
9
0 1 1 0 0 0 0 0 0
1 0 1 1 1 0 0 0 0
1 1 0 1 1 0 0 0 0
0 1 1 0 1 1 0 0 0
0 1 1 1 0 1 0 0 0
0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 1 0 1
0 0 0 0 0 0 1 1 0
1
2
3
4
5
6
7
8
9
1 2 3 4 5 6 7 8 9
FROM
TO
8
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
Adjacency Matrix
|V |
|V |
We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |
8. Graph representations
7
1
3
5
2
4
6
9
0 1 1 0 0 0 0 0 0
1 0 1 1 1 0 0 0 0
1 1 0 1 1 0 0 0 0
0 1 1 0 1 1 0 0 0
0 1 1 1 0 1 0 0 0
0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 1 0 1
0 0 0 0 0 0 1 1 0
1
2
3
4
5
6
7
8
9
1 2 3 4 5 6 7 8 9
FROM
TO
8
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
Adjacency Matrix
|V |
|V |
We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |
9. Graph representations
7
1
3
5
2
4
6
9
0 1 1 0 0 0 0 0 0
1 0 1 1 1 0 0 0 0
1 1 0 1 1 0 0 0 0
0 1 1 0 1 1 0 0 0
0 1 1 1 0 1 0 0 0
0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 1 0 1
0 0 0 0 0 0 1 1 0
1
2
3
4
5
6
7
8
9
1 2 3 4 5 6 7 8 9
FROM
TO
8
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
Adjacency Matrix
|V |
|V |
We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |
10. Graph representations
7
1
3
5
2
4
6
9
0 1 1 0 0 0 0 0 0
1 0 1 1 1 0 0 0 0
1 1 0 1 1 0 0 0 0
0 1 1 0 1 1 0 0 0
0 1 1 1 0 1 0 0 0
0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 1 0 1
0 0 0 0 0 0 1 1 0
1
2
3
4
5
6
7
8
9
1 2 3 4 5 6 7 8 9
FROM
TO
8
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
Adjacency Matrix
|V |
|V |
We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |
11. Graph representations
7
1
3
5
2
4
6
9
1
3
6
7
8
9
1 3 54
1
2
2 54
3 62
3 62
54
8 9
7 9
7 8
3
8
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
Adjacency List
(using linked lists)
We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |
|V | 5 4
4 5
2
12. Graph representations
7
1
3
5
2
4
6
9
1
3
6
7
8
9
1 3 54
1
2
2 54
3 62
3 62
54
8 9
7 9
7 8
3
8
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
Adjacency List
(using linked lists)
We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |
|V | 5 4
4 5
2
13. Graph representations
7
1
3
5
2
4
6
9
1
3
6
7
8
9
1 3 54
1
2
2 54
3 62
3 62
54
8 9
7 9
7 8
3
8
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
Adjacency List
(using linked lists)
We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |
|V | 5 4
4 5
2
14. Graph representations
7
1
3
5
2
4
6
9
1
3
6
7
8
9
1 3 54
1
2
2 54
3 62
3 62
54
8 9
7 9
7 8
3
8
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
Adjacency List
(using linked lists)
We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |
|V | 5 4
4 5
2
15. Graph representations
7
1
3
5
2
4
6
9
1
3
6
7
8
9
1 3 54
1
2
2 54
3 62
3 62
54
8 9
7 9
7 8
3
8
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
Adjacency List
(using linked lists)
We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |
|V | 5 4
4 5
2
16. Graph representations
7
1
3
5
2
4
6
9
1
3
6
7
8
9
1 3 54
1
2
2 54
3 62
3 62
54
8 9
7 9
7 8
3
8
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
Adjacency List
(using linked lists)
both representations are symmetric because the graphs are undirected
We will in general assume that the vertices are numbered 1, 2, 3 . . . |V |
|V | 5 4
4 5
2
17. Basic operations
Adjacency Matrix
Adjacency Matrix
Adjacency List
(using linked lists)
Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
18. Basic operations
Adjacency Matrix
Adjacency Matrix
Adjacency List
(using linked lists)
Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space Θ(|V |2) Θ(|V | + |E|)
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
19. Basic operations
Adjacency Matrix
Adjacency Matrix
Adjacency List
(using linked lists)
Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space Θ(|V |2) Θ(|V | + |E|)
O(1) time
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
20. Basic operations
Adjacency Matrix
Adjacency Matrix
Adjacency List
(using linked lists)
Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space Θ(|V |2) Θ(|V | + |E|)
O(1) time O(V ) time
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
21. Basic operations
Adjacency Matrix
Adjacency Matrix
Adjacency List
(using linked lists)
Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space Θ(|V |2) Θ(|V | + |E|)
O(1) time O(deg(u)) time
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
22. Basic operations
Adjacency Matrix
Adjacency Matrix
Adjacency List
(using linked lists)
Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space Θ(|V |2) Θ(|V | + |E|)
O(1) time O(deg(u)) time
deg(u) (the degree of u),
is the number of edges leaving u
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
23. Basic operations
Adjacency Matrix
Adjacency Matrix
Adjacency List
(using linked lists)
Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space Θ(|V |2) Θ(|V | + |E|)
O(1) time O(deg(u)) time
deg(u) (the degree of u),
is the number of edges leaving u
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
some vertex u
24. Basic operations
Adjacency Matrix
Adjacency Matrix
Adjacency List
(using linked lists)
Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space Θ(|V |2) Θ(|V | + |E|)
O(1) time O(deg(u)) time
deg(u) (the degree of u),
is the number of edges leaving u
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
some vertex u
deg(u) = 8
25. Basic operations
Adjacency Matrix
Adjacency Matrix
Adjacency List
(using linked lists)
Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space Θ(|V |2) Θ(|V | + |E|)
O(1) time O(deg(u)) time
deg(u) (the degree of u),
is the number of edges leaving u
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
26. Basic operations
Adjacency Matrix
Adjacency Matrix
Adjacency List
(using linked lists)
Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space Θ(|V |2) Θ(|V | + |E|)
O(1) time
O(V ) time
O(deg(u)) time
deg(u) (the degree of u),
is the number of edges leaving u
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
27. Basic operations
Adjacency Matrix
Adjacency Matrix
Adjacency List
(using linked lists)
Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space Θ(|V |2) Θ(|V | + |E|)
O(1) time
O(V ) time O(deg(u)) time
O(deg(u)) time
deg(u) (the degree of u),
is the number of edges leaving u
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
28. Basic operations
Adjacency Matrix
Adjacency Matrix
Adjacency List
(using linked lists)
Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space
Adjacency List
(using hash tables)
Θ(|V |2) Θ(|V | + |E|)
O(1) time
O(V ) time O(deg(u)) time
O(deg(u)) time
deg(u) (the degree of u),
is the number of edges leaving u
Θ(|V | + |E|)
O(deg(u)) time
O(1) time
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
Adjacency List
(using hash tables)
1
2
3
4
5
29. Basic operations
all three representations work for directed and/or weighted graphs too
Adjacency Matrix
Adjacency Matrix
Adjacency List
(using linked lists)
Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space
Adjacency List
(using hash tables)
Θ(|V |2) Θ(|V | + |E|)
O(1) time
O(V ) time O(deg(u)) time
O(deg(u)) time
deg(u) (the degree of u),
is the number of edges leaving u
Θ(|V | + |E|)
O(deg(u)) time
O(1) time
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
(with the same complexities)
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
Adjacency List
(using hash tables)
1
2
3
4
5
30. Basic operations
all three representations work for directed and/or weighted graphs too
Adjacency Matrix
Adjacency Matrix
Adjacency List
(using linked lists)
Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space
Adjacency List
(using hash tables)
Θ(|V |2) Θ(|V | + |E|)
O(1) time
O(V ) time O(deg(u)) time
O(deg(u)) time
deg(u) (the degree of u),
is the number of edges leaving u
Θ(|V | + |E|)
O(deg(u)) time
O(1) time
why don’t we always use these?
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
(with the same complexities)
1
2
3
4
5
1
1
2
2
3
2
3
3
4
2
Adjacency List
(using hash tables)
1
2
3
4
5
31. Basic operations
Adjacency Matrix Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space
Adjacency List
(using hash tables)
Θ(|V |2) Θ(|V | + |E|)
O(1) time
O(V ) time O(deg(u)) time
O(deg(u)) time
Θ(|V | + |E|)
O(deg(u)) time
O(1) time
why don’t we always use these?
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
32. Basic operations
Adjacency Matrix Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space
Adjacency List
(using hash tables)
Θ(|V |2) Θ(|V | + |E|)
O(1) time
O(V ) time O(deg(u)) time
O(deg(u)) time
Θ(|V | + |E|)
O(deg(u)) time
O(1) time
why don’t we always use these?
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
Basic hash tables give expected time complexities (constant time ‘on average’)
- no worst case guarantees against collisions
33. Basic operations
Adjacency Matrix Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space
Adjacency List
(using hash tables)
Θ(|V |2) Θ(|V | + |E|)
O(1) time
O(V ) time O(deg(u)) time
O(deg(u)) time
Θ(|V | + |E|)
O(deg(u)) time
O(1) time
why don’t we always use these?
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
Basic hash tables give expected time complexities (constant time ‘on average’)
- no worst case guarantees against collisions
If you’re interested in hashing with provable guarantees,
come to COMS31900 (Advanced Algorithms)
34. Basic operations
Adjacency Matrix Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space
Adjacency List
(using hash tables)
Θ(|V |2) Θ(|V | + |E|)
O(1) time
O(V ) time O(deg(u)) time
O(deg(u)) time
Θ(|V | + |E|)
O(deg(u)) time
O(1) time
why don’t we always use these?
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
Basic hash tables give expected time complexities (constant time ‘on average’)
- no worst case guarantees against collisions
If you’re interested in hashing with provable guarantees,
come to COMS31900 (Advanced Algorithms)
Fortunately, few algorithms need to ask “is there an edge?”
Normally, “tell me all the edges” is fine
35. Basic operations
Adjacency Matrix Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space
Adjacency List
(using hash tables)
Θ(|V |2) Θ(|V | + |E|)
O(1) time
O(V ) time O(deg(u)) time
O(deg(u)) time
Θ(|V | + |E|)
O(deg(u)) time
O(1) time
why don’t we always use these?
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
36. Basic operations
Adjacency Matrix Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space
Adjacency List
(using hash tables)
Θ(|V |2) Θ(|V | + |E|)
O(1) time
O(V ) time O(deg(u)) time
O(deg(u)) time
Θ(|V | + |E|)
O(deg(u)) time
O(1) time
why don’t we always use these?
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
For this course we’ll stick to Adjacency Matrices and Lists (using linked lists)
(for today just Adjacency Lists)
37. Basic operations
Adjacency Matrix Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space
Adjacency List
(using hash tables)
Θ(|V |2) Θ(|V | + |E|)
O(1) time
O(V ) time O(deg(u)) time
O(deg(u)) time
Θ(|V | + |E|)
O(deg(u)) time
O(1) time
why don’t we always use these?
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
For this course we’ll stick to Adjacency Matrices and Lists (using linked lists)
(for today just Adjacency Lists)
One practical reason to stick to standard representations is that
you may not have control over how your graph is stored.
38. Basic operations
Adjacency Matrix Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space
Adjacency List
(using hash tables)
Θ(|V |2) Θ(|V | + |E|)
O(1) time
O(V ) time O(deg(u)) time
O(deg(u)) time
Θ(|V | + |E|)
O(deg(u)) time
O(1) time
why don’t we always use these?
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
For this course we’ll stick to Adjacency Matrices and Lists (using linked lists)
(for today just Adjacency Lists)
One practical reason to stick to standard representations is that
you may not have control over how your graph is stored.
it may not even be stored as a graph...
39. Basic operations
Adjacency Matrix Adjacency List
(using linked lists)
Is there an edge from vertex u to v?
List all the edges leaving u ∈ V
Space
Adjacency List
(using hash tables)
Θ(|V |2) Θ(|V | + |E|)
O(1) time
O(V ) time O(deg(u)) time
O(deg(u)) time
Θ(|V | + |E|)
O(deg(u)) time
O(1) time
why don’t we always use these?
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
For this course we’ll stick to Adjacency Matrices and Lists (using linked lists)
(for today just Adjacency Lists)
One practical reason to stick to standard representations is that
you may not have control over how your graph is stored.
it may not even be stored as a graph...
Fortunately in many cases, you can pretend your graph is stored as one of the above.
46. Intersection Graphs
each intersection
is an edge
we can pretend we have
an Adjacency Matrix
without building one
even if we only store the circles,
the intersections of these circles
correspond to the graph below
47. Other intersection graphs. . .
(these are 1D intervals on a line spread out for visual clarity)
both of these ‘collections’ also
correspond to the example graph
48. Other intersection graphs. . .
= an edge
= a vertex
(these are 1D intervals on a line spread out for visual clarity)
both of these ‘collections’ also
correspond to the example graph
49. Other intersection graphs. . .
= an edge
= a vertex
there is an edge if two intervals intersect
each interval is a vertex
(these are 1D intervals on a line spread out for visual clarity)
both of these ‘collections’ also
correspond to the example graph
50. Other intersection graphs. . .
= an edge
= a vertex
there is an edge if two intervals intersect
each interval is a vertex
(these are 1D intervals on a line spread out for visual clarity)
we can pretend we have
an Adjacency Matrix
without building one
Again,
both of these ‘collections’ also
correspond to the example graph
51. Configuration Graphs
This is the configuration graph for the
(4 disk) towers of Hanoi
a node is a state that the
game can be in
there is an edge if you can get from
one state to another in a single move
52. Configuration Graphs
This is the configuration graph for the
(4 disk) towers of Hanoi
a node is a state that the
game can be in
there is an edge if you can get from
one state to another in a single move
53. Configuration Graphs
This is the configuration graph for the
(4 disk) towers of Hanoi
a node is a state that the
game can be in
there is an edge if you can get from
one state to another in a single move
54. Configuration Graphs
This is the configuration graph for the
(4 disk) towers of Hanoi
a node is a state that the
game can be in
there is an edge if you can get from
one state to another in a single move
55. Configuration Graphs
This is the configuration graph for the
(4 disk) towers of Hanoi
a node is a state that the
game can be in
there is an edge if you can get from
one state to another in a single move
56. Configuration Graphs
This is the configuration graph for the
(4 disk) towers of Hanoi
a node is a state that the
game can be in
there is an edge if you can get from
one state to another in a single move
57. Configuration Graphs
This is the configuration graph for the
(4 disk) towers of Hanoi
a node is a state that the
game can be in
there is an edge if you can get from
one state to another in a single move
58. Configuration Graphs
This is the configuration graph for the
(4 disk) towers of Hanoi
a node is a state that the
game can be in
there is an edge if you can get from
one state to another in a single move
59. Configuration Graphs
This is the configuration graph for the
(4 disk) towers of Hanoi
a node is a state that the
game can be in
there is an edge if you can get from
one state to another in a single move
60. Configuration Graphs
This is the configuration graph for the
(4 disk) towers of Hanoi
a node is a state that the
game can be in
there is an edge if you can get from
one state to another in a single move
storing this uses much less space than
storing this this graph
61. Configuration Graphs
This is the configuration graph for the
(4 disk) towers of Hanoi
a node is a state that the
game can be in
there is an edge if you can get from
one state to another in a single move
storing this uses much less space than
storing this this graph
we can pretend we have
an Adjacency List
without building one
63. Exploring a graph
you are here
BAG Goal: visit every vertex in a
connected graph efficiently
64. Exploring a graph
you are here
BAG Goal: visit every vertex in a
connected graph efficiently
The bag (secretly a data structure)
We can put a vertex in the bag
We can take a vertex from the bag
We don’t care how it works (for now)
(the bag doesn’t forget or change things)
but we don’t know which one we’ll get
65. Exploring a graph
BAG Goal: visit every vertex in a
connected graph efficiently
We going to use an adjacency list so the
graph is really stored like this:
The bag (secretly a data structure)
We can put a vertex in the bag
We can take a vertex from the bag
We don’t care how it works (for now)
(the bag doesn’t forget or change things)
but we don’t know which one we’ll get
66. Exploring a graph
you are here
BAG Goal: visit every vertex in a
connected graph efficiently
67. Exploring a graph
vertex s
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
68. Exploring a graph
vertex s
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
69. Exploring a graph
vertex s
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
70. Exploring a graph
vertex s
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
71. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
72. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
73. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
74. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
75. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
back in!
in twice!
76. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
77. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
78. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
79. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
already marked
(do nothing)
80. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
81. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
82. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
83. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
84. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
85. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
86. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
87. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
Some time later. . .
88. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
Some time later. . . ?
89. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Goal: visit every vertex in a
connected graph efficiently
Some time later. . . ?
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
3. How fast is it? (in the worst case)
90. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
3. How fast is it? (in the worst case)
91. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
1. Does TRAVERSE always stop?
92. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
1. Does TRAVERSE always stop?
93. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
back in!
in twice!
1. Does TRAVERSE always stop?
94. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
back in!
in twice!
1. Does TRAVERSE always stop?
We may put a vertex in the bag many times but each edge is only
processed twice (once in each direction)
95. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
back in!
in twice!
1. Does TRAVERSE always stop?
We may put a vertex in the bag many times but each edge is only
processed twice (once in each direction)
never seen
again!
96. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
back in!
in twice!
1. Does TRAVERSE always stop?
We may put a vertex in the bag many times but each edge is only
processed twice (once in each direction)
never seen
again!
Important later: number of ‘puts’ 2|E|
97. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
back in!
in twice!
1. Does TRAVERSE always stop?
We may put a vertex in the bag many times but each edge is only
processed twice (once in each direction)
never seen
again!
Important later: number of ‘puts’ 2|E|
TRAVERSE(s)
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
98. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
back in!
in twice!
1. Does TRAVERSE always stop?
We may put a vertex in the bag many times but each edge is only
processed twice (once in each direction)
never seen
again!
Important later: number of ‘puts’ 2|E|
99. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
back in!
in twice!
1. Does TRAVERSE always stop? Yes
We may put a vertex in the bag many times but each edge is only
processed twice (once in each direction)
never seen
again!
Important later: number of ‘puts’ 2|E|
100. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
3. How fast is it? (in the worst case)
101. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
102. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
Proof (by induction)
(unless it was already marked)
103. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
104. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
105. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
106. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
107. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
108. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
109. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
110. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
111. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
112. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
there could be a ‘shortcut’
(unless it was already marked)
113. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
there could be a ‘shortcut’
(unless it was already marked)
114. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
there could be a ‘shortcut’
(unless it was already marked)
115. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
there could be a ‘shortcut’
(unless it was already marked)
116. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
there could be a ‘shortcut’
(unless it was already marked)
117. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
there could be a ‘shortcut’
(unless it was already marked)
118. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
119. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
120. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
121. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
122. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
123. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
124. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
(unless it was already marked)
125. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
Yes
(unless it was already marked)
126. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
Yes
The correctness doesn’t depend
on how the bag works!
(unless it was already marked)
127. Exploring a graph
BAG
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
2. Does it always visit every vertex?
When a vertex on this path is marked, the next vertex is put in the bag
Eventually, that vertex is removed from the bag and is marked.
Consider any path from s to some v:
s
v
Proof (by induction)
Yes
The correctness doesn’t depend
on how the bag works!
(but this doesn’t tell you anything about
which order vertices are marked in)
(unless it was already marked)
128. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
129. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Assumption
(we’ll come back to this)
bag operations
take O(1) time
130. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Assumption
(we’ll come back to this)
bag operations
take O(1) time
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
131. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Assumption
(we’ll come back to this)
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
bag operations
take O(1) time
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
132. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Time complexity:
Assumption
(we’ll come back to this)
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
bag operations
take O(1) time
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
133. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Time complexity:
Assumption
(we’ll come back to this)
O(1) time every time we take from the bag
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
bag operations
take O(1) time
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
134. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Time complexity:
Assumption
(we’ll come back to this)
(store an array where mark[u] = 1 iff u is marked)
O(1) time every time we take from the bag
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
bag operations
take O(1) time
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
135. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Time complexity:
Assumption
(we’ll come back to this)
(store an array where mark[u] = 1 iff u is marked)
O(1) time every time we take from the bag
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
bag operations
take O(1) time
O(1) time every time we put into the bag
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
136. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Time complexity:
Assumption
(we’ll come back to this)
(store an array where mark[u] = 1 iff u is marked)
O(1) time every time we take from the bag
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
bag operations
take O(1) time
O(1) time every time we put into the bag
What is the total number of bag operations?
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
137. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Time complexity:
Assumption
(we’ll come back to this)
(store an array where mark[u] = 1 iff u is marked)
O(1) time every time we take from the bag
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
bag operations
take O(1) time
O(1) time every time we put into the bag
What is the total number of bag operations?
we do at most 2|E| put operations
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
138. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Time complexity:
Assumption
(we’ll come back to this)
(store an array where mark[u] = 1 iff u is marked)
O(1) time every time we take from the bag
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
bag operations
take O(1) time
O(1) time every time we put into the bag
What is the total number of bag operations?
we do at most 2|E| put operations
so we do at most 2|E| take operations
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
139. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Time complexity:
Assumption
(we’ll come back to this)
(store an array where mark[u] = 1 iff u is marked)
O(1) time every time we take from the bag
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
bag operations
take O(1) time
O(1) time every time we put into the bag
What is the total number of bag operations?
we do at most 2|E| put operations
so we do at most 2|E| take operations
The overall time complexity is O(|E|)
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
140. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Time complexity:
Assumption
(we’ll come back to this)
(store an array where mark[u] = 1 iff u is marked)
O(1) time every time we take from the bag
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
bag operations
take O(1) time
O(1) time every time we put into the bag
What is the total number of bag operations?
we do at most 2|E| put operations
so we do at most 2|E| take operations
The overall time complexity is O(|E|)
O(|E|)
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
141. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Assumption
(we’ll come back to this)
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
bag operations
take O(1) time
O(|E|)
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
142. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Assumption
(we’ll come back to this)
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
bag operations
take O(1) time
O(|E|)
What if we had used an
adjacency matrix?
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
143. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Assumption
(we’ll come back to this)
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
bag operations
take O(1) time
O(|E|)
What if we had used an
adjacency matrix?
finding all edges leaving u would have taken O(|V |) time
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
144. Exploring a graph
Questions:
1. Does TRAVERSE always stop?
2. Does it always visit every vertex?
Yes
Yes
3. How fast is it? (in the worst case)
Assumption
(we’ll come back to this)
put s into the bag
while the bag is not empty
take u from the bag
if u unmarked
mark u
for every edge (u, v)
put v into the bag
TRAVERSE(s)
bag operations
take O(1) time
O(|E|)
What if we had used an
adjacency matrix?
finding all edges leaving u would have taken O(|V |) time
0 1 1 0 0
1 0 1 1 1
1 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1
2
3
4
5
1 2 3 4 5
overall, the time complexity would have been O(|V |2) instead of O(|E|).
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges
145. How should we implement the bag?
BAG
Queue Stack
Operations take O(1) time
Depth First SearchBreadth First Search
but in different orders with different types of bag
TRAVERSE visits every vertex in a connected graph in O(|E|) time
with a Queue the algorithm is called with a Stack the algorithm is called
Applications Applications
Shortest paths in unweighted graphs
Max-Flow
Testing whether a graph is bipartite
Finding (strongly) connected components
Topicologically sorting a Directed Acyclic Graph
Testing for planarity
146. How should we implement the bag?
BAG
Queue Stack
Operations take O(1) time
Depth First SearchBreadth First Search
but in different orders with different types of bag
TRAVERSE visits every vertex in a connected graph in O(|E|) time
with a Queue the algorithm is called with a Stack the algorithm is called
Applications Applications
Shortest paths in unweighted graphs
Max-Flow
Testing whether a graph is bipartite
Finding (strongly) connected components
Topicologically sorting a Directed Acyclic Graph
Testing for planarity
(and it works for directed graphs too)
147. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
148. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
distance = 1
149. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
distance = 3
150. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
distance = 6
151. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
152. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
153. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
154. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
155. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
156. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
157. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
158. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
159. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
160. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
161. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
162. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
these are all in front of these
in the queue
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
163. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
these are all in front of these
in the queue
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
164. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
165. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
166. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
167. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
168. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
169. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
170. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
171. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
172. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
This is why it’s called
Breadth-First Search
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
173. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
This is why it’s called
Breadth-First Search
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Proof by induction (on the distance from s)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
174. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
This is why it’s called
Breadth-First Search
Base Case: BFS marks all vertices at distance 0 from s
before marking any vertex at distance > 0 .
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Proof by induction (on the distance from s)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
175. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
This is why it’s called
Breadth-First Search
Base Case: BFS marks all vertices at distance 0 from s
before marking any vertex at distance > 0 .
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Proof by induction (on the distance from s)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
176. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
This is why it’s called
Breadth-First Search
Base Case: BFS marks all vertices at distance 0 from s
before marking any vertex at distance > 0 .
Inductive Hypothesis: Assume that BFS marks all vertices at distance (i − 1)
before marking any vertex at distance > (i − 1).
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Proof by induction (on the distance from s)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
177. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
This is why it’s called
Breadth-First Search
Base Case: BFS marks all vertices at distance 0 from s
before marking any vertex at distance > 0 .
Inductive Hypothesis: Assume that BFS marks all vertices at distance (i − 1)
before marking any vertex at distance > (i − 1).
When BFS marks a vertex at distance (i − 1),
it puts all its neighbours in the queue.
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Proof by induction (on the distance from s)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
178. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
This is why it’s called
Breadth-First Search
Base Case: BFS marks all vertices at distance 0 from s
before marking any vertex at distance > 0 .
Inductive Hypothesis: Assume that BFS marks all vertices at distance (i − 1)
before marking any vertex at distance > (i − 1).
When BFS marks a vertex at distance (i − 1),
it puts all its neighbours in the queue.
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Proof by induction (on the distance from s)
v is u’s neighbour iff
(u, v) ∈ E
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
179. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
This is why it’s called
Breadth-First Search
Base Case: BFS marks all vertices at distance 0 from s
before marking any vertex at distance > 0 .
Inductive Hypothesis: Assume that BFS marks all vertices at distance (i − 1)
before marking any vertex at distance > (i − 1).
When BFS marks a vertex at distance (i − 1),
Immediately after marking all vertices at distance (i − 1):
it puts all its neighbours in the queue.
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Proof by induction (on the distance from s)
v is u’s neighbour iff
(u, v) ∈ E
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
180. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
This is why it’s called
Breadth-First Search
Base Case: BFS marks all vertices at distance 0 from s
before marking any vertex at distance > 0 .
Inductive Hypothesis: Assume that BFS marks all vertices at distance (i − 1)
before marking any vertex at distance > (i − 1).
When BFS marks a vertex at distance (i − 1),
Immediately after marking all vertices at distance (i − 1):
it puts all its neighbours in the queue.
every vertex at distance i is in the queue.
no vertex at distance > i has been put in the queue.
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Proof by induction (on the distance from s)
v is u’s neighbour iff
(u, v) ∈ E
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
181. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
This is why it’s called
Breadth-First Search
Base Case: BFS marks all vertices at distance 0 from s
before marking any vertex at distance > 0 .
Inductive Hypothesis: Assume that BFS marks all vertices at distance (i − 1)
before marking any vertex at distance > (i − 1).
When BFS marks a vertex at distance (i − 1),
Immediately after marking all vertices at distance (i − 1):
it puts all its neighbours in the queue.
every vertex at distance i is in the queue.
no vertex at distance > i has been put in the queue.
because we haven’t marked any of its neighbours
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Proof by induction (on the distance from s)
(they are all at distance > (i − 1))
v is u’s neighbour iff
(u, v) ∈ E
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
182. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
This is why it’s called
Breadth-First Search
Base Case: BFS marks all vertices at distance 0 from s
before marking any vertex at distance > 0 .
Inductive Hypothesis: Assume that BFS marks all vertices at distance (i − 1)
before marking any vertex at distance > (i − 1).
When BFS marks a vertex at distance (i − 1),
Immediately after marking all vertices at distance (i − 1):
it puts all its neighbours in the queue.
every vertex at distance i is in the queue.
no vertex at distance > i has been put in the queue.
because we haven’t marked any of its neighbours
Therefore, all vertices at distance i will be marked before any vertex at distance > i
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Proof by induction (on the distance from s)
(they are all at distance > (i − 1))
v is u’s neighbour iff
(u, v) ∈ E
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
183. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
184. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
we can use this to find the shortest path
from s to every other vertex in O(|V | + |E|) time
With a little bit of book-keeping,
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
185. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
This also works for directed, unweighted graphs
we can use this to find the shortest path
from s to every other vertex in O(|V | + |E|) time
With a little bit of book-keeping,
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
186. Shortest paths in unweighted graphs
s
Goal: find the distance from the
source s to every other vertex
Breadth-First Search (BFS)
is TRAVERSE using a queue as the bag
Lemma In BFS, the vertices
are marked in distance order (smallest first)
This also works for directed, unweighted graphs
we can use this to find the shortest path
from s to every other vertex in O(|V | + |E|) time
With a little bit of book-keeping,
It does not work for weighted graphs
TRAVERSE(s) - with a queue
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
187. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
188. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s How does this affect the time complexity?
189. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
Time complexity:
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s How does this affect the time complexity?
190. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
Time complexity:
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s How does this affect the time complexity?
O(|V |) time to initialise an array dist
191. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
Time complexity:
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s How does this affect the time complexity?
O(|V |) time to initialise an array dist
O(1) time to set the distance to s
192. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
Time complexity:
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s How does this affect the time complexity?
O(|V |) time to initialise an array dist
O(1) time to set the distance to s
O(1) time whenever we put into the queue
(so this doesn’t change the complexity)
193. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
Time complexity:
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s How does this affect the time complexity?
O(|V |) time to initialise an array dist
O(1) time to set the distance to s
O(1) time whenever we put into the queue
(so this doesn’t change the complexity)
so the overall complexity is O(|V | + |E|)
194. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
195. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
Why does this work?
196. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
Why does this work?
Lemma In BFS, the vertices are marked in
distance order (smallest first)
197. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
Why does this work?
Correctness sketch: (using the Lemma)
Lemma In BFS, the vertices are marked in
distance order (smallest first)
198. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
Why does this work?
Correctness sketch: (using the Lemma)
For each v, dist(v) is set when it is first ‘discovered’
and inserted into the queue. (and it never changes)
Lemma In BFS, the vertices are marked in
distance order (smallest first)
199. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
Why does this work?
Correctness sketch: (using the Lemma)
For each v, dist(v) is set when it is first ‘discovered’
and inserted into the queue. (and it never changes)
Let u be the vertex which first ‘discovered’ v
Lemma In BFS, the vertices are marked in
distance order (smallest first)
200. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
Why does this work?
Correctness sketch: (using the Lemma)
For each v, dist(v) is set when it is first ‘discovered’
and inserted into the queue. (and it never changes)
Let u be the vertex which first ‘discovered’ v
Lemma In BFS, the vertices are marked in
distance order (smallest first)
BFS sets dist(v) = dist(u) + 1
201. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
Why does this work?
Correctness sketch: (using the Lemma)
For each v, dist(v) is set when it is first ‘discovered’
and inserted into the queue. (and it never changes)
Let u be the vertex which first ‘discovered’ v
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Assume for a contradiction that there is a path
from s to v with length < dist(u) + 1
BFS sets dist(v) = dist(u) + 1
202. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
Why does this work?
Correctness sketch: (using the Lemma)
For each v, dist(v) is set when it is first ‘discovered’
and inserted into the queue. (and it never changes)
Let u be the vertex which first ‘discovered’ v
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Assume for a contradiction that there is a path
from s to v with length < dist(u) + 1
BFS sets dist(v) = dist(u) + 1
length < dist(u) + 1
s
v
203. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
Why does this work?
Correctness sketch: (using the Lemma)
For each v, dist(v) is set when it is first ‘discovered’
and inserted into the queue. (and it never changes)
Let u be the vertex which first ‘discovered’ v
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Let x be the previous vertex on this path
Assume for a contradiction that there is a path
from s to v with length < dist(u) + 1
BFS sets dist(v) = dist(u) + 1
length < dist(u) + 1
s
v
204. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
Why does this work?
Correctness sketch: (using the Lemma)
For each v, dist(v) is set when it is first ‘discovered’
and inserted into the queue. (and it never changes)
Let u be the vertex which first ‘discovered’ v
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Let x be the previous vertex on this path
Assume for a contradiction that there is a path
from s to v with length < dist(u) + 1
BFS sets dist(v) = dist(u) + 1
length < dist(u) + 1
s
v
x
205. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
Why does this work?
Correctness sketch: (using the Lemma)
For each v, dist(v) is set when it is first ‘discovered’
and inserted into the queue. (and it never changes)
Let u be the vertex which first ‘discovered’ v
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Let x be the previous vertex on this path
Assume for a contradiction that there is a path
from s to v with length < dist(u) + 1
BFS sets dist(v) = dist(u) + 1
length < dist(u) length = 1
s
v
x
206. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
Why does this work?
Correctness sketch: (using the Lemma)
For each v, dist(v) is set when it is first ‘discovered’
and inserted into the queue. (and it never changes)
Let u be the vertex which first ‘discovered’ v
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Let x be the previous vertex on this path
Assume for a contradiction that there is a path
from s to v with length < dist(u) + 1
BFS sets dist(v) = dist(u) + 1
length < dist(u) length = 1
s
v
x
207. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
Why does this work?
Correctness sketch: (using the Lemma)
For each v, dist(v) is set when it is first ‘discovered’
and inserted into the queue. (and it never changes)
Let u be the vertex which first ‘discovered’ v
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Let x be the previous vertex on this path
Assume for a contradiction that there is a path
from s to v with length < dist(u) + 1
x has distance < dist(u) so was marked before u
(by the Lemma) and ‘discovered’ v first
BFS sets dist(v) = dist(u) + 1
length < dist(u) length = 1
s
v
x
208. Shortest Paths using BFS
for all v, set dist(v) = ∞ (NEW!)
set dist(s) = 0 (NEW!)
put s into the queue
while the queue is not empty
take u from the queue
if u unmarked
mark u
for every edge (u, v)
put v into the queue
if dist(v) = ∞ (NEW!)
dist(v) = dist(u) + 1 (NEW!)
BFS(s)
dist(v) gives the distance
between s and v
We’ve added four (NEW!) lines to TRAVERSE
to track the distances from s
Why does this work?
Correctness sketch: (using the Lemma)
For each v, dist(v) is set when it is first ‘discovered’
and inserted into the queue. (and it never changes)
Let u be the vertex which first ‘discovered’ v
Lemma In BFS, the vertices are marked in
distance order (smallest first)
Let x be the previous vertex on this path
Assume for a contradiction that there is a path
from s to v with length < dist(u) + 1
x has distance < dist(u) so was marked before u
(by the Lemma) and ‘discovered’ v first
Contradiction!
BFS sets dist(v) = dist(u) + 1
length < dist(u) length = 1
s
v
x
209. Conclusion
Depth First Search (DFS)Breadth First Search (BFS)
- the traversal order depends on the type of bag
TRAVERSE visits every vertex in a connected graph in O(|E|) time
with a Queue the algorithm is called with a Stack the algorithm is called
Applications Applications
Shortest paths in unweighted graphs
Max-Flow
Testing whether a graph is bipartite
Finding (strongly) connected components
Topicologically sorting a Directed Acyclic Graph
Testing for planarity
(with an O(1) time bag)
take O(|V | + |E|) time using BFS
Question
What does TRAVERSE do on an
unconnected graph?(works for directed graphs too)
(and it works for directed graphs too)
V is the set of vertices
|V | is the number of vertices
E is the set of edges
|E| is the number of edges