The document provides an introduction to graph theory. It lists prescribed and recommended books, outlines topics that will be covered including history, definitions, types of graphs, terminology, representation, subgraphs, connectivity, and applications. It notes that the Government of India designated June 10th as Graph Theory Day in recognition of the influence and importance of graph theory.
Euler proved that it is impossible to traverse all the bridges in Königsberg once each by inventing the mathematical graph. He showed that the Königsberg bridge network has 4 vertices, all with an odd number of edges, meaning it is not possible to start and end at the same location using each bridge only once. Adding an 8th bridge would make it possible by creating only 2 odd vertices. A graph is traversable if it has 0 or 2 vertices with an odd number of edges.
The document describes the Konigsberg bridge problem and its solution by Euler. It discusses that Konigsberg (now Kaliningrad) had islands connected by seven bridges, and residents wondered if one could walk across each bridge once and return to their starting point. Euler proved this was impossible by modeling the problem as a graph with four vertices (land areas) and seven edges (bridges). He discovered all four vertices were odd degrees, meaning they were not traversable. The document also discusses how adding bridges could make the graph traversable and introduces basic graph theory concepts.
This document discusses key concepts in graph theory and algorithms for finding paths in graphs:
- It defines different types of graphs like simple graphs, directed graphs, and bipartite graphs.
- It explains representations of graphs using adjacency matrices and lists.
- It covers graph isomorphism, connected graphs, Euler paths and circuits, and Hamiltonian paths and cycles.
- It introduces the shortest path problem and Dijkstra's algorithm for finding the shortest path between vertices in a weighted graph.
- It provides an example applying Dijkstra's algorithm to find the shortest path between two vertices.
1) The document introduces basic concepts in graph theory including definitions of graphs, vertices, edges and examples.
2) It discusses some classic problems in graph theory including Euler's solving of the Seven Bridges of Königsberg problem, which helped formulate graph theory.
3) The document defines Euler paths, Euler circuits and the conditions for when a graph contains them based on the number of odd and even vertices.
The document provides information about a faculty development program on discrete mathematics. It includes:
- An outline of the course content which covers topics like graph terminology, representations of graphs, connectivity, Euler and Hamilton paths, shortest path algorithms, planar graphs, and graph coloring.
- Details of learning resources including textbooks and reference books.
- A table listing the topics to be discussed in lectures, related self-learning tasks, reference materials and number of contact hours.
- Information on representation of graphs through adjacency matrix, incidence matrix and adjacency lists.
The program aims to teach key concepts in graph theory and related algorithms over 6 lecture hours through lectures, self-study and reference books.
This document discusses applications of graph theory in human life and provides examples of graph theory concepts. It describes how graph theory is used in modeling wireless communication networks like GSM. Specifically, it explains that frequency assignment in GSM networks can be modeled as a graph coloring problem, where each cell/region is assigned one of four frequencies/colors such that adjacent cells have different colors. It also discusses how other graph problems like the traveling salesman problem and vertex coloring have practical applications in areas like logistics and network design.
The document provides an introduction to graph theory. It lists prescribed and recommended books, outlines topics that will be covered including history, definitions, types of graphs, terminology, representation, subgraphs, connectivity, and applications. It notes that the Government of India designated June 10th as Graph Theory Day in recognition of the influence and importance of graph theory.
Euler proved that it is impossible to traverse all the bridges in Königsberg once each by inventing the mathematical graph. He showed that the Königsberg bridge network has 4 vertices, all with an odd number of edges, meaning it is not possible to start and end at the same location using each bridge only once. Adding an 8th bridge would make it possible by creating only 2 odd vertices. A graph is traversable if it has 0 or 2 vertices with an odd number of edges.
The document describes the Konigsberg bridge problem and its solution by Euler. It discusses that Konigsberg (now Kaliningrad) had islands connected by seven bridges, and residents wondered if one could walk across each bridge once and return to their starting point. Euler proved this was impossible by modeling the problem as a graph with four vertices (land areas) and seven edges (bridges). He discovered all four vertices were odd degrees, meaning they were not traversable. The document also discusses how adding bridges could make the graph traversable and introduces basic graph theory concepts.
This document discusses key concepts in graph theory and algorithms for finding paths in graphs:
- It defines different types of graphs like simple graphs, directed graphs, and bipartite graphs.
- It explains representations of graphs using adjacency matrices and lists.
- It covers graph isomorphism, connected graphs, Euler paths and circuits, and Hamiltonian paths and cycles.
- It introduces the shortest path problem and Dijkstra's algorithm for finding the shortest path between vertices in a weighted graph.
- It provides an example applying Dijkstra's algorithm to find the shortest path between two vertices.
1) The document introduces basic concepts in graph theory including definitions of graphs, vertices, edges and examples.
2) It discusses some classic problems in graph theory including Euler's solving of the Seven Bridges of Königsberg problem, which helped formulate graph theory.
3) The document defines Euler paths, Euler circuits and the conditions for when a graph contains them based on the number of odd and even vertices.
The document provides information about a faculty development program on discrete mathematics. It includes:
- An outline of the course content which covers topics like graph terminology, representations of graphs, connectivity, Euler and Hamilton paths, shortest path algorithms, planar graphs, and graph coloring.
- Details of learning resources including textbooks and reference books.
- A table listing the topics to be discussed in lectures, related self-learning tasks, reference materials and number of contact hours.
- Information on representation of graphs through adjacency matrix, incidence matrix and adjacency lists.
The program aims to teach key concepts in graph theory and related algorithms over 6 lecture hours through lectures, self-study and reference books.
This document discusses applications of graph theory in human life and provides examples of graph theory concepts. It describes how graph theory is used in modeling wireless communication networks like GSM. Specifically, it explains that frequency assignment in GSM networks can be modeled as a graph coloring problem, where each cell/region is assigned one of four frequencies/colors such that adjacent cells have different colors. It also discusses how other graph problems like the traveling salesman problem and vertex coloring have practical applications in areas like logistics and network design.
This document provides an introduction to basic graph theory terminology and concepts. It defines a graph as a data structure consisting of nodes and edges that relate the nodes. Examples of graphs include networks, electronic circuits, and navigation systems. Classic problems in graph theory are discussed, such as the Seven Bridges of Königsberg problem solved by Euler that established graph theory. Euler's theory on Euler paths and circuits is explained along with formal definitions of graphs, vertices, edges, and examples. Terminology including adjacent vertices, endpoints, and origins of edges are also defined.
This document discusses graphs and graph theory. It begins by defining a graph as a pictorial representation consisting of vertices and edges. It then discusses key graph theory concepts such as the degree of a vertex, distance between vertices, eccentricity of a vertex, and the radius of a graph. Finally, it provides examples of different types of graphs and a link to a Java implementation of graphs.
This document contains lecture materials on data structures and algorithms from SRM Institute of Science and Technology, Chennai. It discusses topics related to graphs including terminology, representations, classifications, traversals and spanning trees. Specific graph algorithms covered include depth first search, breadth first search, minimum spanning trees using Prim's and Kruskal's algorithms. It also lists some learning resources and provides examples to explain various graph concepts.
This document provides an introduction to topological graph theory. It begins with definitions of basic graph theory concepts from a topological perspective, such as representing graphs with curved arcs instead of straight lines. It then discusses graph drawings, incidence matrices, vertex valence, and graph maps/isomorphisms. Important classes of graphs are introduced, such as trees, paths, cycles, and complete graphs. The document aims to introduce preliminary concepts of topological graph theory to students in a simple manner.
The Age of EulerRarely has the world seen a mathematician as pro.docxmehek4
The Age of Euler
Rarely has the world seen a mathematician as prolific as the great Leonhard Euler1 (1707-1783). Born in Switzerland, he eventually obtained royal appointments in two European courts, namely Russia and Germany (under Frederick the Great). He published so many mathematics articles that his work fills seventy thick volumes. His publications account for one-third of all the technical articles of eighteenth-century Europe. The preceding century saw the rise of scientific and mathematical journals – the new media of the times and the quickest way of making innovations known to colleagues across the continent. This outgrowth of the printing revolution of the fifteenth century accelerated the pace of mathematical and scientific progress by transmitting new ideas in a timely manner – much like the present computer revolution has just begun to affect the dissemination of knowledge.
1Euler was the person who gave us the notation π for pi, i for , Δy for the change in y, f (x) for a function, and Σ for summation.
After Euler’s death, it took forty years for the backlog of his work to appear in print. Although he lost his sight in 1768, for the last fifteen years of his life he continued his research at his usual energetic pace while his students copied his pearls of wisdom. It is inconceivable to most how he did mathematics without pencil and paper – without being able to see the multitude of diagrams, equations, and graphs needed to do research.
What areas of math did he enrich and expand? The question is what field of math did he not enrich and expand! Not only did he contribute substantially to calculus, geometry, algebra, and number theory, he also invented several fields. Though a father to eleven children, Euler found time to become the father of an important branch of mathematics, known today as graph theory, which would be important in modern fields such as computer science and operations research, as well as traditional areas such as physics and chemistry.
Euler became the father of graph theory as well as topology after solving the notorious “Seven Bridges of Königsberg” problem. The diagram of Figure 10-1 shows the four landmasses of the city of Königsberg and the seven bridges interconnecting them.
Figure 10-1
The problem was to devise a route that traverses each bridge exactly once and to end where one starts. Euler observed that the task could not be done!! He noticed that each landmass has an odd number of bridges connecting it with the rest of the city. Hence a traveler departing, returning, departing, and so forth, an odd number of times would wind up departing on the last bridge, rendering impossible his return to his point of origin.
Let’s consider this gem of thinking one more time. Number the bridges contiguous with landmassA, 1, 2, and 3. Then if one starts the trip by departing A on bridge number one, he must return on bridge number two or number three, leaving only one more bridge. Clearly he must depart on that ...
Random graphs are mathematical models that use probability distributions to represent complex networks. They lie at the intersection of graph theory and probability theory. The most commonly studied random graph model is the Erdős–Rényi model, also known as G(n,p), where n is the number of vertices and p is the probability of an edge between any two vertices. In G(n,p), each possible edge occurs independently with probability p, allowing for analysis of average properties like the diameter as the graph size increases.
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.
APPLICATIONS OF GRAPH THEORY IN HUMAN LIFEMary Calkins
1. The document discusses applications of graph theory in designing a GSM network operated by Bharat Sanchar Nigam Limited. Graph theory techniques are used to determine the base station identity code, hopping sequence number, and location area code.
2. Graph theory is also applied to solve optimization and scheduling problems in GSM radio network planning. Classical applications of graph theory concepts like graph coloring, Eulerian graphs, and Hamiltonian graphs are discussed.
3. The origins of graph theory are traced back to problems like the Koinsberg bridge problem in the 18th century. Key developments that helped establish graph theory as a field are highlighted, including work in the 19th century on planar graphs, trees, and
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.
ON ALGORITHMIC PROBLEMS CONCERNING GRAPHS OF HIGHER DEGREE OF SYMMETRYFransiskeran
Since the ancient determination of the five platonic solids the study of symmetry and regularity has always
been one of the most fascinating aspects of mathematics. One intriguing phenomenon of studies in graph
theory is the fact that quite often arithmetic regularity properties of a graph imply the existence of many
symmetries, i.e. large automorphism group G. In some important special situation higher degree of
regularity means that G is an automorphism group of finite geometry. For example, a glance through the
list of distance regular graphs of diameter d < 3 reveals the fact that most of them are connected with
classical Lie geometry. Theory of distance regular graphs is an important part of algebraic combinatorics
and its applications such as coding theory, communication networks, and block design. An important tool
for investigation of such graphs is their spectra, which is the set of eigenvalues of adjacency matrix of a
graph. Let G be a finite simple group of Lie type and X be the set homogeneous elements of the associated
geometry.
The document introduces fundamental concepts in graph theory. It discusses graphs as models to represent relationships between objects using vertices and edges. A graph is defined as a triple consisting of a vertex set, edge set, and relation between edges and vertex pairs. Different graph types are introduced, including simple graphs, loops, multiple edges, paths, cycles, and complete graphs. Adjacency, incidence, and vertex degree are also defined.
Skiena algorithm 2007 lecture10 graph data strctureszukun
This document summarizes different types of graph data structures and representations. It discusses graphs as consisting of vertices and edges, and describes properties like directed vs undirected, weighted vs unweighted, sparse vs dense. It also covers different representations like adjacency matrices and lists, and provides code examples for initializing, reading, and inserting edges into a graph represented using an adjacency list.
The document defines key terms related to graphs and networks. It provides examples of different types of graphs like trees, paths, and bipartite graphs. It also describes algorithms like Prim's and Kruskal's that are used to find the minimum spanning tree of a weighted network. Prim's algorithm works by greedily adding the lowest weight edge that connects an unconnected node. Kruskal's algorithm sorts all edges by weight and only adds an edge if it does not create a cycle. Both algorithms can be applied to distance matrices to find the minimum spanning tree.
Graphs are non-linear data structures that can be used to represent relationships between objects. A graph contains vertices (nodes) connected by edges (links). A graph G is defined as a set of vertices V connected by a set of edges E. For example, a graph G can be defined as G = (V, E) where V = {A,B,C,D,E} and E = {(A,B), (A,C), (A,D), (B,D), (C,D), (B,E), (E,D)}. Graphs can model real-world networks like roads connecting cities. Euler used graphs to solve the Königsberg bridge problem by showing
Graph theory concepts complex networks presents-rouhollah nabatinabati
This document provides an introduction to network and social network analysis theory, including basic concepts of graph theory and network structures. It defines what a network and graph are, explains what network theory techniques are used for, and gives examples of real-world networks that can be represented as graphs. It also summarizes key graph theory concepts such as nodes, edges, walks, paths, cycles, connectedness, degree, and centrality measures.
Dr. M. Usha of the Department of Computer Science and Engineering at Velammal Engineering College gave a presentation on graphs. A graph is a non-linear data structure consisting of nodes and edges. Graphs can be used to represent networks and are applied in areas like social networks. Common graph terminology includes vertices, edges, paths, cycles, and graph representations like adjacency matrices and adjacency lists. Graph traversal algorithms like depth-first search and breadth-first search are used to systematically visit all nodes.
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.
This document discusses using knowledge graphs for textual fact checking. It defines a knowledge graph as a directed graph with nodes representing entities and edges representing relationships. Knowledge graphs can be used for fact checking by constructing a knowledge stream between the subject and object of a statement using multiple paths in the graph. The document proposes using flow networks and minimum cost maximum flow algorithms to compute the knowledge stream and determine a truth score for evaluating statements. It also discusses using relational similarity between edges and predicates to improve existing fact checking approaches.
This document provides an introduction to basic graph theory terminology and concepts. It defines a graph as a data structure consisting of nodes and edges that relate the nodes. Examples of graphs include networks, electronic circuits, and navigation systems. Classic problems in graph theory are discussed, such as the Seven Bridges of Königsberg problem solved by Euler that established graph theory. Euler's theory on Euler paths and circuits is explained along with formal definitions of graphs, vertices, edges, and examples. Terminology including adjacent vertices, endpoints, and origins of edges are also defined.
This document discusses graphs and graph theory. It begins by defining a graph as a pictorial representation consisting of vertices and edges. It then discusses key graph theory concepts such as the degree of a vertex, distance between vertices, eccentricity of a vertex, and the radius of a graph. Finally, it provides examples of different types of graphs and a link to a Java implementation of graphs.
This document contains lecture materials on data structures and algorithms from SRM Institute of Science and Technology, Chennai. It discusses topics related to graphs including terminology, representations, classifications, traversals and spanning trees. Specific graph algorithms covered include depth first search, breadth first search, minimum spanning trees using Prim's and Kruskal's algorithms. It also lists some learning resources and provides examples to explain various graph concepts.
This document provides an introduction to topological graph theory. It begins with definitions of basic graph theory concepts from a topological perspective, such as representing graphs with curved arcs instead of straight lines. It then discusses graph drawings, incidence matrices, vertex valence, and graph maps/isomorphisms. Important classes of graphs are introduced, such as trees, paths, cycles, and complete graphs. The document aims to introduce preliminary concepts of topological graph theory to students in a simple manner.
The Age of EulerRarely has the world seen a mathematician as pro.docxmehek4
The Age of Euler
Rarely has the world seen a mathematician as prolific as the great Leonhard Euler1 (1707-1783). Born in Switzerland, he eventually obtained royal appointments in two European courts, namely Russia and Germany (under Frederick the Great). He published so many mathematics articles that his work fills seventy thick volumes. His publications account for one-third of all the technical articles of eighteenth-century Europe. The preceding century saw the rise of scientific and mathematical journals – the new media of the times and the quickest way of making innovations known to colleagues across the continent. This outgrowth of the printing revolution of the fifteenth century accelerated the pace of mathematical and scientific progress by transmitting new ideas in a timely manner – much like the present computer revolution has just begun to affect the dissemination of knowledge.
1Euler was the person who gave us the notation π for pi, i for , Δy for the change in y, f (x) for a function, and Σ for summation.
After Euler’s death, it took forty years for the backlog of his work to appear in print. Although he lost his sight in 1768, for the last fifteen years of his life he continued his research at his usual energetic pace while his students copied his pearls of wisdom. It is inconceivable to most how he did mathematics without pencil and paper – without being able to see the multitude of diagrams, equations, and graphs needed to do research.
What areas of math did he enrich and expand? The question is what field of math did he not enrich and expand! Not only did he contribute substantially to calculus, geometry, algebra, and number theory, he also invented several fields. Though a father to eleven children, Euler found time to become the father of an important branch of mathematics, known today as graph theory, which would be important in modern fields such as computer science and operations research, as well as traditional areas such as physics and chemistry.
Euler became the father of graph theory as well as topology after solving the notorious “Seven Bridges of Königsberg” problem. The diagram of Figure 10-1 shows the four landmasses of the city of Königsberg and the seven bridges interconnecting them.
Figure 10-1
The problem was to devise a route that traverses each bridge exactly once and to end where one starts. Euler observed that the task could not be done!! He noticed that each landmass has an odd number of bridges connecting it with the rest of the city. Hence a traveler departing, returning, departing, and so forth, an odd number of times would wind up departing on the last bridge, rendering impossible his return to his point of origin.
Let’s consider this gem of thinking one more time. Number the bridges contiguous with landmassA, 1, 2, and 3. Then if one starts the trip by departing A on bridge number one, he must return on bridge number two or number three, leaving only one more bridge. Clearly he must depart on that ...
Random graphs are mathematical models that use probability distributions to represent complex networks. They lie at the intersection of graph theory and probability theory. The most commonly studied random graph model is the Erdős–Rényi model, also known as G(n,p), where n is the number of vertices and p is the probability of an edge between any two vertices. In G(n,p), each possible edge occurs independently with probability p, allowing for analysis of average properties like the diameter as the graph size increases.
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.
APPLICATIONS OF GRAPH THEORY IN HUMAN LIFEMary Calkins
1. The document discusses applications of graph theory in designing a GSM network operated by Bharat Sanchar Nigam Limited. Graph theory techniques are used to determine the base station identity code, hopping sequence number, and location area code.
2. Graph theory is also applied to solve optimization and scheduling problems in GSM radio network planning. Classical applications of graph theory concepts like graph coloring, Eulerian graphs, and Hamiltonian graphs are discussed.
3. The origins of graph theory are traced back to problems like the Koinsberg bridge problem in the 18th century. Key developments that helped establish graph theory as a field are highlighted, including work in the 19th century on planar graphs, trees, and
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.
ON ALGORITHMIC PROBLEMS CONCERNING GRAPHS OF HIGHER DEGREE OF SYMMETRYFransiskeran
Since the ancient determination of the five platonic solids the study of symmetry and regularity has always
been one of the most fascinating aspects of mathematics. One intriguing phenomenon of studies in graph
theory is the fact that quite often arithmetic regularity properties of a graph imply the existence of many
symmetries, i.e. large automorphism group G. In some important special situation higher degree of
regularity means that G is an automorphism group of finite geometry. For example, a glance through the
list of distance regular graphs of diameter d < 3 reveals the fact that most of them are connected with
classical Lie geometry. Theory of distance regular graphs is an important part of algebraic combinatorics
and its applications such as coding theory, communication networks, and block design. An important tool
for investigation of such graphs is their spectra, which is the set of eigenvalues of adjacency matrix of a
graph. Let G be a finite simple group of Lie type and X be the set homogeneous elements of the associated
geometry.
The document introduces fundamental concepts in graph theory. It discusses graphs as models to represent relationships between objects using vertices and edges. A graph is defined as a triple consisting of a vertex set, edge set, and relation between edges and vertex pairs. Different graph types are introduced, including simple graphs, loops, multiple edges, paths, cycles, and complete graphs. Adjacency, incidence, and vertex degree are also defined.
Skiena algorithm 2007 lecture10 graph data strctureszukun
This document summarizes different types of graph data structures and representations. It discusses graphs as consisting of vertices and edges, and describes properties like directed vs undirected, weighted vs unweighted, sparse vs dense. It also covers different representations like adjacency matrices and lists, and provides code examples for initializing, reading, and inserting edges into a graph represented using an adjacency list.
The document defines key terms related to graphs and networks. It provides examples of different types of graphs like trees, paths, and bipartite graphs. It also describes algorithms like Prim's and Kruskal's that are used to find the minimum spanning tree of a weighted network. Prim's algorithm works by greedily adding the lowest weight edge that connects an unconnected node. Kruskal's algorithm sorts all edges by weight and only adds an edge if it does not create a cycle. Both algorithms can be applied to distance matrices to find the minimum spanning tree.
Graphs are non-linear data structures that can be used to represent relationships between objects. A graph contains vertices (nodes) connected by edges (links). A graph G is defined as a set of vertices V connected by a set of edges E. For example, a graph G can be defined as G = (V, E) where V = {A,B,C,D,E} and E = {(A,B), (A,C), (A,D), (B,D), (C,D), (B,E), (E,D)}. Graphs can model real-world networks like roads connecting cities. Euler used graphs to solve the Königsberg bridge problem by showing
Graph theory concepts complex networks presents-rouhollah nabatinabati
This document provides an introduction to network and social network analysis theory, including basic concepts of graph theory and network structures. It defines what a network and graph are, explains what network theory techniques are used for, and gives examples of real-world networks that can be represented as graphs. It also summarizes key graph theory concepts such as nodes, edges, walks, paths, cycles, connectedness, degree, and centrality measures.
Dr. M. Usha of the Department of Computer Science and Engineering at Velammal Engineering College gave a presentation on graphs. A graph is a non-linear data structure consisting of nodes and edges. Graphs can be used to represent networks and are applied in areas like social networks. Common graph terminology includes vertices, edges, paths, cycles, and graph representations like adjacency matrices and adjacency lists. Graph traversal algorithms like depth-first search and breadth-first search are used to systematically visit all nodes.
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.
This document discusses using knowledge graphs for textual fact checking. It defines a knowledge graph as a directed graph with nodes representing entities and edges representing relationships. Knowledge graphs can be used for fact checking by constructing a knowledge stream between the subject and object of a statement using multiple paths in the graph. The document proposes using flow networks and minimum cost maximum flow algorithms to compute the knowledge stream and determine a truth score for evaluating statements. It also discusses using relational similarity between edges and predicates to improve existing fact checking approaches.
Mobile App Development Company In Noida | Drona InfotechDrona Infotech
React.js, a JavaScript library developed by Facebook, has gained immense popularity for building user interfaces, especially for single-page applications. Over the years, React has evolved and expanded its capabilities, becoming a preferred choice for mobile app development. This article will explore why React.js is an excellent choice for the Best Mobile App development company in Noida.
Visit Us For Information: https://www.linkedin.com/pulse/what-makes-reactjs-stand-out-mobile-app-development-rajesh-rai-pihvf/
Orca: Nocode Graphical Editor for Container OrchestrationPedro J. Molina
Tool demo on CEDI/SISTEDES/JISBD2024 at A Coruña, Spain. 2024.06.18
"Orca: Nocode Graphical Editor for Container Orchestration"
by Pedro J. Molina PhD. from Metadev
Streamlining End-to-End Testing Automation with Azure DevOps Build & Release Pipelines
Automating end-to-end (e2e) test for Android and iOS native apps, and web apps, within Azure build and release pipelines, poses several challenges. This session dives into the key challenges and the repeatable solutions implemented across multiple teams at a leading Indian telecom disruptor, renowned for its affordable 4G/5G services, digital platforms, and broadband connectivity.
Challenge #1. Ensuring Test Environment Consistency: Establishing a standardized test execution environment across hundreds of Azure DevOps agents is crucial for achieving dependable testing results. This uniformity must seamlessly span from Build pipelines to various stages of the Release pipeline.
Challenge #2. Coordinated Test Execution Across Environments: Executing distinct subsets of tests using the same automation framework across diverse environments, such as the build pipeline and specific stages of the Release Pipeline, demands flexible and cohesive approaches.
Challenge #3. Testing on Linux-based Azure DevOps Agents: Conducting tests, particularly for web and native apps, on Azure DevOps Linux agents lacking browser or device connectivity presents specific challenges in attaining thorough testing coverage.
This session delves into how these challenges were addressed through:
1. Automate the setup of essential dependencies to ensure a consistent testing environment.
2. Create standardized templates for executing API tests, API workflow tests, and end-to-end tests in the Build pipeline, streamlining the testing process.
3. Implement task groups in Release pipeline stages to facilitate the execution of tests, ensuring consistency and efficiency across deployment phases.
4. Deploy browsers within Docker containers for web application testing, enhancing portability and scalability of testing environments.
5. Leverage diverse device farms dedicated to Android, iOS, and browser testing to cover a wide range of platforms and devices.
6. Integrate AI technology, such as Applitools Visual AI and Ultrafast Grid, to automate test execution and validation, improving accuracy and efficiency.
7. Utilize AI/ML-powered central test automation reporting server through platforms like reportportal.io, providing consolidated and real-time insights into test performance and issues.
These solutions not only facilitate comprehensive testing across platforms but also promote the principles of shift-left testing, enabling early feedback, implementing quality gates, and ensuring repeatability. By adopting these techniques, teams can effectively automate and execute tests, accelerating software delivery while upholding high-quality standards across Android, iOS, and web applications.
Ensuring Efficiency and Speed with Practical Solutions for Clinical OperationsOnePlan Solutions
Clinical operations professionals encounter unique challenges. Balancing regulatory requirements, tight timelines, and the need for cross-functional collaboration can create significant internal pressures. Our upcoming webinar will introduce key strategies and tools to streamline and enhance clinical development processes, helping you overcome these challenges.
The Rising Future of CPaaS in the Middle East 2024Yara Milbes
Explore "The Rising Future of CPaaS in the Middle East in 2024" with this comprehensive PPT presentation. Discover how Communication Platforms as a Service (CPaaS) is transforming communication across various sectors in the Middle East.
Secure-by-Design Using Hardware and Software Protection for FDA ComplianceICS
This webinar explores the “secure-by-design” approach to medical device software development. During this important session, we will outline which security measures should be considered for compliance, identify technical solutions available on various hardware platforms, summarize hardware protection methods you should consider when building in security and review security software such as Trusted Execution Environments for secure storage of keys and data, and Intrusion Detection Protection Systems to monitor for threats.
Transforming Product Development using OnePlan To Boost Efficiency and Innova...OnePlan Solutions
Ready to overcome challenges and drive innovation in your organization? Join us in our upcoming webinar where we discuss how to combat resource limitations, scope creep, and the difficulties of aligning your projects with strategic goals. Discover how OnePlan can revolutionize your product development processes, helping your team to innovate faster, manage resources more effectively, and deliver exceptional results.
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...Luigi Fugaro
Vector databases are transforming how we handle data, allowing us to search through text, images, and audio by converting them into vectors. Today, we'll dive into the basics of this exciting technology and discuss its potential to revolutionize our next-generation AI applications. We'll examine typical uses for these databases and the essential tools
developers need. Plus, we'll zoom in on the advanced capabilities of vector search and semantic caching in Java, showcasing these through a live demo with Redis libraries. Get ready to see how these powerful tools can change the game!
Enhanced Screen Flows UI/UX using SLDS with Tom KittPeter Caitens
Join us for an engaging session led by Flow Champion, Tom Kitt. This session will dive into a technique of enhancing the user interfaces and user experiences within Screen Flows using the Salesforce Lightning Design System (SLDS). This technique uses Native functionality, with No Apex Code, No Custom Components and No Managed Packages required.
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISTier1 app
Are you ready to unlock the secrets hidden within Java thread dumps? Join us for a hands-on session where we'll delve into effective troubleshooting patterns to swiftly identify the root causes of production problems. Discover the right tools, techniques, and best practices while exploring *real-world case studies of major outages* in Fortune 500 enterprises. Engage in interactive lab exercises where you'll have the opportunity to troubleshoot thread dumps and uncover performance issues firsthand. Join us and become a master of Java thread dump analysis!
What is Continuous Testing in DevOps - A Definitive Guide.pdfkalichargn70th171
Once an overlooked aspect, continuous testing has become indispensable for enterprises striving to accelerate application delivery and reduce business impacts. According to a Statista report, 31.3% of global enterprises have embraced continuous integration and deployment within their DevOps, signaling a pervasive trend toward hastening release cycles.
Consistent toolbox talks are critical for maintaining workplace safety, as they provide regular opportunities to address specific hazards and reinforce safe practices.
These brief, focused sessions ensure that safety is a continual conversation rather than a one-time event, which helps keep safety protocols fresh in employees' minds. Studies have shown that shorter, more frequent training sessions are more effective for retention and behavior change compared to longer, infrequent sessions.
Engaging workers regularly, toolbox talks promote a culture of safety, empower employees to voice concerns, and ultimately reduce the likelihood of accidents and injuries on site.
The traditional method of conducting safety talks with paper documents and lengthy meetings is not only time-consuming but also less effective. Manual tracking of attendance and compliance is prone to errors and inconsistencies, leading to gaps in safety communication and potential non-compliance with OSHA regulations. Switching to a digital solution like Safelyio offers significant advantages.
Safelyio automates the delivery and documentation of safety talks, ensuring consistency and accessibility. The microlearning approach breaks down complex safety protocols into manageable, bite-sized pieces, making it easier for employees to absorb and retain information.
This method minimizes disruptions to work schedules, eliminates the hassle of paperwork, and ensures that all safety communications are tracked and recorded accurately. Ultimately, using a digital platform like Safelyio enhances engagement, compliance, and overall safety performance on site. https://safelyio.com/
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...kalichargn70th171
Visual testing plays a vital role in ensuring that software products meet the aesthetic requirements specified by clients in functional and non-functional specifications. In today's highly competitive digital landscape, users expect a seamless and visually appealing online experience. Visual testing, also known as automated UI testing or visual regression testing, verifies the accuracy of the visual elements that users interact with.
5. IRIS H.-R. JIANG
Salute to Euler!
¨ Our focus in this course is on problems with a discrete flavor.
¨ One of the most fundamental and expressive of combinatorial
structures is the graph.
¤ Invented by L. Euler based on his proof on the Königsberg bridge
problem (the seven bridge problem) in 1736.
n Is it possible to walk across all the bridges exactly once and
return to the starting land area?
n Abstraction!
Graphs
5
No
Eulerian
walk
L. Euler, Solutioproblematis ad geometriamsituspertinentis,
Commentarii Academiae Scientiarum Imperialis Petropolitanae,
Vol. 8, pp. 128—140, 1736 (published 1741).
IRIS H.-R. JIANG
Graph
¨ A graph is simply a way of encoding pairwise relationships
among a set of objects.
¨ A graphG = (V, E) consists of
¤ A collection V of nodes (a.k.a. vertices)
¤ A collection E of edges
n Each edge joins two nodes
n e = {u, v} Î E for some u, vÎ V
¨ In an undirected graph: symmetric relationships
¤ Edges are undirected, i.e., {u, v} == {v, u}
n e.g., u and v are family.
¨ In a directed graph: asymmetric relationships
¤ Edges are directed, i.e., (u, v) != (v, u)
n e.g., u knows v (celebrity), while v doesn’t know u.
¨ v is one of u’sneighbor if there is an edge (u, v)
¤ Adjacency
Graphs
6
u v
u v
tail head
⚾婾㗗ᶨ攨㍊妶䈑ẞᷳ攻⤪ỽ䚠忋䘬⬠⓷
ᶫ㧳⓷柴㛔岒⌛㗗㍊妶映⛘ᷳ攻(德忶㧳㠩)䘬忋䳸斄Ὢ
IRIS H.-R. JIANG
Examples of Graphs (1/6)
¨ It’s useful to digest the meaning of the nodes and the meaning
of the edges in the following examples.
¤ It’s not important to remember them.
¨ Transportation networks:
Graphs
7
London underground map
(node: station; edge: adjacent stations)
ᷴ㔾䵺⅟娔姯—‒㕍✗搜✗⛽ (H. Beck, 1933)
http://mag.udn.com/mag/world/storypage.jsp?f_ART_ID=207102
China Airlines international route map
(node: city; edge: non-stop flight)
IRIS H.-R. JIANG
Examples of Graphs (2/6)
¨ Communication networks
Graphs
8
Wireless sensor network
(node: sensor; edge: signal broadcasting)
6. IRIS H.-R. JIANG
Salute to Euler!
¨ Our focus in this course is on problems with a discrete flavor.
¨ One of the most fundamental and expressive of combinatorial
structures is the graph.
¤ Invented by L. Euler based on his proof on the Königsberg bridge
problem (the seven bridge problem) in 1736.
n Is it possible to walk across all the bridges exactly once and
return to the starting land area?
n Abstraction!
Graphs
5
No
Eulerian
walk
L. Euler, Solutioproblematis ad geometriamsituspertinentis,
Commentarii Academiae Scientiarum Imperialis Petropolitanae,
Vol. 8, pp. 128—140, 1736 (published 1741).
IRIS H.-R. JIANG
Graph
¨ A graph is simply a way of encoding pairwise relationships
among a set of objects.
¨ A graphG = (V, E) consists of
¤ A collection V of nodes (a.k.a. vertices)
¤ A collection E of edges
n Each edge joins two nodes
n e = {u, v} Î E for some u, vÎ V
¨ In an undirected graph: symmetric relationships
¤ Edges are undirected, i.e., {u, v} == {v, u}
n e.g., u and v are family.
¨ In a directed graph: asymmetric relationships
¤ Edges are directed, i.e., (u, v) != (v, u)
n e.g., u knows v (celebrity), while v doesn’t know u.
¨ v is one of u’sneighbor if there is an edge (u, v)
¤ Adjacency
Graphs
6
u v
u v
tail head
⚾婾㗗ᶨ攨㍊妶䈑ẞᷳ攻⤪ỽ䚠忋䘬⬠⓷
ᶫ㧳⓷柴㛔岒⌛㗗㍊妶映⛘ᷳ攻(德忶㧳㠩)䘬忋䳸斄Ὢ
IRIS H.-R. JIANG
Examples of Graphs (1/6)
¨ It’s useful to digest the meaning of the nodes and the meaning
of the edges in the following examples.
¤ It’s not important to remember them.
¨ Transportation networks:
Graphs
7
London underground map
(node: station; edge: adjacent stations)
ᷴ㔾䵺⅟娔姯—‒㕍✗搜✗⛽ (H. Beck, 1933)
http://mag.udn.com/mag/world/storypage.jsp?f_ART_ID=207102
China Airlines international route map
(node: city; edge: non-stop flight)
IRIS H.-R. JIANG
Examples of Graphs (2/6)
¨ Communication networks
Graphs
8
Wireless sensor network
(node: sensor; edge: signal broadcasting)
7. IRIS H.-R. JIANG
Salute to Euler!
¨ Our focus in this course is on problems with a discrete flavor.
¨ One of the most fundamental and expressive of combinatorial
structures is the graph.
¤ Invented by L. Euler based on his proof on the Königsberg bridge
problem (the seven bridge problem) in 1736.
n Is it possible to walk across all the bridges exactly once and
return to the starting land area?
n Abstraction!
Graphs
5
No
Eulerian
walk
L. Euler, Solutioproblematis ad geometriamsituspertinentis,
Commentarii Academiae Scientiarum Imperialis Petropolitanae,
Vol. 8, pp. 128—140, 1736 (published 1741).
IRIS H.-R. JIANG
Graph
¨ A graph is simply a way of encoding pairwise relationships
among a set of objects.
¨ A graphG = (V, E) consists of
¤ A collection V of nodes (a.k.a. vertices)
¤ A collection E of edges
n Each edge joins two nodes
n e = {u, v} Î E for some u, vÎ V
¨ In an undirected graph: symmetric relationships
¤ Edges are undirected, i.e., {u, v} == {v, u}
n e.g., u and v are family.
¨ In a directed graph: asymmetric relationships
¤ Edges are directed, i.e., (u, v) != (v, u)
n e.g., u knows v (celebrity), while v doesn’t know u.
¨ v is one of u’sneighbor if there is an edge (u, v)
¤ Adjacency
Graphs
6
u v
u v
tail head
⚾婾㗗ᶨ攨㍊妶䈑ẞᷳ攻⤪ỽ䚠忋䘬⬠⓷
ᶫ㧳⓷柴㛔岒⌛㗗㍊妶映⛘ᷳ攻(德忶㧳㠩)䘬忋䳸斄Ὢ
IRIS H.-R. JIANG
Examples of Graphs (1/6)
¨ It’s useful to digest the meaning of the nodes and the meaning
of the edges in the following examples.
¤ It’s not important to remember them.
¨ Transportation networks:
Graphs
7
London underground map
(node: station; edge: adjacent stations)
ᷴ㔾䵺⅟娔姯—‒㕍✗搜✗⛽ (H. Beck, 1933)
http://mag.udn.com/mag/world/storypage.jsp?f_ART_ID=207102
China Airlines international route map
(node: city; edge: non-stop flight)
IRIS H.-R. JIANG
Examples of Graphs (2/6)
¨ Communication networks
Graphs
8
Wireless sensor network
(node: sensor; edge: signal broadcasting)
8. IRIS H.-R. JIANG
Salute to Euler!
¨ Our focus in this course is on problems with a discrete flavor.
¨ One of the most fundamental and expressive of combinatorial
structures is the graph.
¤ Invented by L. Euler based on his proof on the Königsberg bridge
problem (the seven bridge problem) in 1736.
n Is it possible to walk across all the bridges exactly once and
return to the starting land area?
n Abstraction!
Graphs
5
No
Eulerian
walk
L. Euler, Solutioproblematis ad geometriamsituspertinentis,
Commentarii Academiae Scientiarum Imperialis Petropolitanae,
Vol. 8, pp. 128—140, 1736 (published 1741).
IRIS H.-R. JIANG
Graph
¨ A graph is simply a way of encoding pairwise relationships
among a set of objects.
¨ A graphG = (V, E) consists of
¤ A collection V of nodes (a.k.a. vertices)
¤ A collection E of edges
n Each edge joins two nodes
n e = {u, v} Î E for some u, vÎ V
¨ In an undirected graph: symmetric relationships
¤ Edges are undirected, i.e., {u, v} == {v, u}
n e.g., u and v are family.
¨ In a directed graph: asymmetric relationships
¤ Edges are directed, i.e., (u, v) != (v, u)
n e.g., u knows v (celebrity), while v doesn’t know u.
¨ v is one of u’sneighbor if there is an edge (u, v)
¤ Adjacency
Graphs
6
u v
u v
tail head
⚾婾㗗ᶨ攨㍊妶䈑ẞᷳ攻⤪ỽ䚠忋䘬⬠⓷
ᶫ㧳⓷柴㛔岒⌛㗗㍊妶映⛘ᷳ攻(德忶㧳㠩)䘬忋䳸斄Ὢ
IRIS H.-R. JIANG
Examples of Graphs (1/6)
¨ It’s useful to digest the meaning of the nodes and the meaning
of the edges in the following examples.
¤ It’s not important to remember them.
¨ Transportation networks:
Graphs
7
London underground map
(node: station; edge: adjacent stations)
ᷴ㔾䵺⅟娔姯—‒㕍✗搜✗⛽ (H. Beck, 1933)
http://mag.udn.com/mag/world/storypage.jsp?f_ART_ID=207102
China Airlines international route map
(node: city; edge: non-stop flight)
IRIS H.-R. JIANG
Examples of Graphs (2/6)
¨ Communication networks
Graphs
8
Wireless sensor network
(node: sensor; edge: signal broadcasting)
9. IRIS H.-R. JIANG
Examples of Graphs (3/6)
¨ Information networks
Graphs
9
World Wide Web
(node: webpage; edge: hyperlink)
IRIS H.-R. JIANG
Examples of Graphs (4/6)
¨ Social networks
Graphs
10
Facebook
(node: people; edge: friendship)
http://en.wikipedia.org/wiki/Facebook
IRIS H.-R. JIANG
Examples of Graphs (5/6)
¨ Dependency networks
Graphs
11
Food chain/web
(node: species; edge: from prey to predator)
IRIS H.-R. JIANG
Examples of Graphs (6/6)
¨ Technological Network
Graphs
12
Finite state machine
(node: state; edge: state transition)
10. IRIS H.-R. JIANG
Examples of Graphs (3/6)
¨ Information networks
Graphs
9
World Wide Web
(node: webpage; edge: hyperlink)
IRIS H.-R. JIANG
Examples of Graphs (4/6)
¨ Social networks
Graphs
10
Facebook
(node: people; edge: friendship)
http://en.wikipedia.org/wiki/Facebook
IRIS H.-R. JIANG
Examples of Graphs (5/6)
¨ Dependency networks
Graphs
11
Food chain/web
(node: species; edge: from prey to predator)
IRIS H.-R. JIANG
Examples of Graphs (6/6)
¨ Technological Network
Graphs
12
Finite state machine
(node: state; edge: state transition)
11. IRIS H.-R. JIANG
Examples of Graphs (3/6)
¨ Information networks
Graphs
9
World Wide Web
(node: webpage; edge: hyperlink)
IRIS H.-R. JIANG
Examples of Graphs (4/6)
¨ Social networks
Graphs
10
Facebook
(node: people; edge: friendship)
http://en.wikipedia.org/wiki/Facebook
IRIS H.-R. JIANG
Examples of Graphs (5/6)
¨ Dependency networks
Graphs
11
Food chain/web
(node: species; edge: from prey to predator)
IRIS H.-R. JIANG
Examples of Graphs (6/6)
¨ Technological Network
Graphs
12
Finite state machine
(node: state; edge: state transition)
12. IRIS H.-R. JIANG
Examples of Graphs (3/6)
¨ Information networks
Graphs
9
World Wide Web
(node: webpage; edge: hyperlink)
IRIS H.-R. JIANG
Examples of Graphs (4/6)
¨ Social networks
Graphs
10
Facebook
(node: people; edge: friendship)
http://en.wikipedia.org/wiki/Facebook
IRIS H.-R. JIANG
Examples of Graphs (5/6)
¨ Dependency networks
Graphs
11
Food chain/web
(node: species; edge: from prey to predator)
IRIS H.-R. JIANG
Examples of Graphs (6/6)
¨ Technological Network
Graphs
12
Finite state machine
(node: state; edge: state transition)
13. IRIS H.-R. JIANG
Paths and Connectivity (1/2)
¨ One of the fundamental operations in a graph is that of
traversing a sequence of nodes connected by edges.
¤ Browse Web pages by following hyperlinks
¤ Join a 10-day tour from Taipei to Europe on a sequence of flights
¤ Pass gossip by word of mouth (by message of mobile phone)
from you to someone far away
n “Hey upper east siders, Gossip Girl here!”
-- Gossip Girl
Graphs
13
IRIS H.-R. JIANG
Paths and Connectivity (2/2)
¨ A path in an undirected graph G = (V, E) is a sequence P of
nodes v1, v2, …, vk-1, vk with the property that each consecutive
pair vi, vi+1 is joined by an edge in E.
¨ A path is simple if all nodes are distinct.
¨ A cycle is a path v1, v2, …, vk-1, vk in which v1 = vk, k> 2, and the
first k-1 nodes are all distinct.
¨ An undirected graph is connected if, for every pair of nodes u
and v, there is a path from u to v.
¨ The distance between nodes u and v is the minimum number of
edges in a u-v path. (¥ for disconnected)
¨ Note: These definitions carry over naturally to directed graphs
with respect to the directionality of edges.
Graphs
14
Path P = 1, 2, 4, 5, 3, 7, 8
Cycle C = 1, 2, 4, 5, 3, 1
1
2 3
4 5
6
7
8
IRIS H.-R. JIANG
Trees
¨ An undirected graph is a tree if it is connected and does not
contain a cycle.
¤ Trees are the simplest kind of connected graph: deleting any
edge will disconnect it.
¨ Thm: Let G be an undirected graph on n nodes. Any two of the
following statements imply the third.
¤ G is connected.
¤ G does not contain a cycle.
¤ G has n-1 edges.
Graphs
15
2
3
1
4
5
6
7
9
8
2
3
1
4
5
6
7
9
8
Grab 1 and let
the rest hang
downward
Tree? Yes!
G is a tree if it
satisfies any two of
the three statements
IRIS H.-R. JIANG
Rooted Trees
¨ A rooted tree is a tree with its root at r.
¨ Rooted trees encode the notion of a hierarchy.
¤ e.g., sitemap of a Web site
n The tree-like structure facilitates navigation (root: entry page)
Graphs
16
a tree the same tree, rooted at 1
v
v’s parent
v’s child
root r
2
3
1
4
5
6
7
9
8
2
3
1
4
5
6
7
9
8
Grab 1 and let
the rest hang
downward leaf
Directed edges:
asymmetric
relationship
14. IRIS H.-R. JIANG
Paths and Connectivity (1/2)
¨ One of the fundamental operations in a graph is that of
traversing a sequence of nodes connected by edges.
¤ Browse Web pages by following hyperlinks
¤ Join a 10-day tour from Taipei to Europe on a sequence of flights
¤ Pass gossip by word of mouth (by message of mobile phone)
from you to someone far away
n “Hey upper east siders, Gossip Girl here!”
-- Gossip Girl
Graphs
13
IRIS H.-R. JIANG
Paths and Connectivity (2/2)
¨ A path in an undirected graph G = (V, E) is a sequence P of
nodes v1, v2, …, vk-1, vk with the property that each consecutive
pair vi, vi+1 is joined by an edge in E.
¨ A path is simple if all nodes are distinct.
¨ A cycle is a path v1, v2, …, vk-1, vk in which v1 = vk, k> 2, and the
first k-1 nodes are all distinct.
¨ An undirected graph is connected if, for every pair of nodes u
and v, there is a path from u to v.
¨ The distance between nodes u and v is the minimum number of
edges in a u-v path. (¥ for disconnected)
¨ Note: These definitions carry over naturally to directed graphs
with respect to the directionality of edges.
Graphs
14
Path P = 1, 2, 4, 5, 3, 7, 8
Cycle C = 1, 2, 4, 5, 3, 1
1
2 3
4 5
6
7
8
IRIS H.-R. JIANG
Trees
¨ An undirected graph is a tree if it is connected and does not
contain a cycle.
¤ Trees are the simplest kind of connected graph: deleting any
edge will disconnect it.
¨ Thm: Let G be an undirected graph on n nodes. Any two of the
following statements imply the third.
¤ G is connected.
¤ G does not contain a cycle.
¤ G has n-1 edges.
Graphs
15
2
3
1
4
5
6
7
9
8
2
3
1
4
5
6
7
9
8
Grab 1 and let
the rest hang
downward
Tree? Yes!
G is a tree if it
satisfies any two of
the three statements
IRIS H.-R. JIANG
Rooted Trees
¨ A rooted tree is a tree with its root at r.
¨ Rooted trees encode the notion of a hierarchy.
¤ e.g., sitemap of a Web site
n The tree-like structure facilitates navigation (root: entry page)
Graphs
16
a tree the same tree, rooted at 1
v
v’s parent
v’s child
root r
2
3
1
4
5
6
7
9
8
2
3
1
4
5
6
7
9
8
Grab 1 and let
the rest hang
downward leaf
Directed edges:
asymmetric
relationship
15. IRIS H.-R. JIANG
Paths and Connectivity (1/2)
¨ One of the fundamental operations in a graph is that of
traversing a sequence of nodes connected by edges.
¤ Browse Web pages by following hyperlinks
¤ Join a 10-day tour from Taipei to Europe on a sequence of flights
¤ Pass gossip by word of mouth (by message of mobile phone)
from you to someone far away
n “Hey upper east siders, Gossip Girl here!”
-- Gossip Girl
Graphs
13
IRIS H.-R. JIANG
Paths and Connectivity (2/2)
¨ A path in an undirected graph G = (V, E) is a sequence P of
nodes v1, v2, …, vk-1, vk with the property that each consecutive
pair vi, vi+1 is joined by an edge in E.
¨ A path is simple if all nodes are distinct.
¨ A cycle is a path v1, v2, …, vk-1, vk in which v1 = vk, k> 2, and the
first k-1 nodes are all distinct.
¨ An undirected graph is connected if, for every pair of nodes u
and v, there is a path from u to v.
¨ The distance between nodes u and v is the minimum number of
edges in a u-v path. (¥ for disconnected)
¨ Note: These definitions carry over naturally to directed graphs
with respect to the directionality of edges.
Graphs
14
Path P = 1, 2, 4, 5, 3, 7, 8
Cycle C = 1, 2, 4, 5, 3, 1
1
2 3
4 5
6
7
8
IRIS H.-R. JIANG
Trees
¨ An undirected graph is a tree if it is connected and does not
contain a cycle.
¤ Trees are the simplest kind of connected graph: deleting any
edge will disconnect it.
¨ Thm: Let G be an undirected graph on n nodes. Any two of the
following statements imply the third.
¤ G is connected.
¤ G does not contain a cycle.
¤ G has n-1 edges.
Graphs
15
2
3
1
4
5
6
7
9
8
2
3
1
4
5
6
7
9
8
Grab 1 and let
the rest hang
downward
Tree? Yes!
G is a tree if it
satisfies any two of
the three statements
IRIS H.-R. JIANG
Rooted Trees
¨ A rooted tree is a tree with its root at r.
¨ Rooted trees encode the notion of a hierarchy.
¤ e.g., sitemap of a Web site
n The tree-like structure facilitates navigation (root: entry page)
Graphs
16
a tree the same tree, rooted at 1
v
v’s parent
v’s child
root r
2
3
1
4
5
6
7
9
8
2
3
1
4
5
6
7
9
8
Grab 1 and let
the rest hang
downward leaf
Directed edges:
asymmetric
relationship
16. IRIS H.-R. JIANG
Paths and Connectivity (1/2)
¨ One of the fundamental operations in a graph is that of
traversing a sequence of nodes connected by edges.
¤ Browse Web pages by following hyperlinks
¤ Join a 10-day tour from Taipei to Europe on a sequence of flights
¤ Pass gossip by word of mouth (by message of mobile phone)
from you to someone far away
n “Hey upper east siders, Gossip Girl here!”
-- Gossip Girl
Graphs
13
IRIS H.-R. JIANG
Paths and Connectivity (2/2)
¨ A path in an undirected graph G = (V, E) is a sequence P of
nodes v1, v2, …, vk-1, vk with the property that each consecutive
pair vi, vi+1 is joined by an edge in E.
¨ A path is simple if all nodes are distinct.
¨ A cycle is a path v1, v2, …, vk-1, vk in which v1 = vk, k> 2, and the
first k-1 nodes are all distinct.
¨ An undirected graph is connected if, for every pair of nodes u
and v, there is a path from u to v.
¨ The distance between nodes u and v is the minimum number of
edges in a u-v path. (¥ for disconnected)
¨ Note: These definitions carry over naturally to directed graphs
with respect to the directionality of edges.
Graphs
14
Path P = 1, 2, 4, 5, 3, 7, 8
Cycle C = 1, 2, 4, 5, 3, 1
1
2 3
4 5
6
7
8
IRIS H.-R. JIANG
Trees
¨ An undirected graph is a tree if it is connected and does not
contain a cycle.
¤ Trees are the simplest kind of connected graph: deleting any
edge will disconnect it.
¨ Thm: Let G be an undirected graph on n nodes. Any two of the
following statements imply the third.
¤ G is connected.
¤ G does not contain a cycle.
¤ G has n-1 edges.
Graphs
15
2
3
1
4
5
6
7
9
8
2
3
1
4
5
6
7
9
8
Grab 1 and let
the rest hang
downward
Tree? Yes!
G is a tree if it
satisfies any two of
the three statements
IRIS H.-R. JIANG
Rooted Trees
¨ A rooted tree is a tree with its root at r.
¨ Rooted trees encode the notion of a hierarchy.
¤ e.g., sitemap of a Web site
n The tree-like structure facilitates navigation (root: entry page)
Graphs
16
a tree the same tree, rooted at 1
v
v’s parent
v’s child
root r
2
3
1
4
5
6
7
9
8
2
3
1
4
5
6
7
9
8
Grab 1 and let
the rest hang
downward leaf
Directed edges:
asymmetric
relationship
17. BFS
DFS
Graph Connectivity and Graph Traversal
17
Graphs
1
2 3
4 5
6
7
8
9
10
11
12
13
IRIS H.-R. JIANG
Node-to-Node Connectivity
¨ Q: Given a graph G = (V, E) and two particular nodes s and t, is
there a path from s to t in G?
¤ The s-t connectivity problem
¤ The maze-solving problem
¨ A:
¤ For small graphs, easy! (visual inspection)
n 1-6 connectivity? 7-13 connectivity?
¤ What if large graphs? How efficiently can we do?
Graphs
18
1
2 3
4 5
6
7
8
9
10
11
12
13
room
hallway
IRIS H.-R. JIANG
Breadth-First-Search (BFS)
Graphs
19
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
9
10
11
12
13
L0
L1
L2
L3
¨ Breadth-first search (BFS): propagate the waves
¤ Start at s and flood the graph with an expanding wave that grows
to visit all nodes that it can reach.
¤ Layer Li: i is the time that a node is reached.
n Layer L0 = {s}; layer L1 = all neighbors of L0.
n Layer Lj+1 = all nodes that do not belong to an earlier layer and
that are neighbors of Lj.
n i = distance between s to the nodes that belong to layer Li.
Adjacent nodes
IRIS H.-R. JIANG
BFS Tree
Graphs
20
L0
L1
L2
L3 1
2 3
4 5
6
7
8
Nontree edge
Tree edge
BFS tree
¨ Let T be a BFS tree, let x and y be nodes in T belonging to
layers Li and Lj respectively, and let (x, y) be an edge of G. Then
i and j differ by at most 1.
¨ Pf:
¤ Without loss of generality, suppose j – i > 1.
¤ By definition, xÎLi , x’s neighbors belongs to Li+1 or earlier.
¤ Since (x, y) is an edge of G, y is x’s neighbor, yÎ Lj and j ! i+1.
¤ ®¬
1
2 3
4 5
6
8
7
L0
L1
L2
L3
BFS tree
18. BFS
DFS
Graph Connectivity and Graph Traversal
17
Graphs
1
2 3
4 5
6
7
8
9
10
11
12
13
IRIS H.-R. JIANG
Node-to-Node Connectivity
¨ Q: Given a graph G = (V, E) and two particular nodes s and t, is
there a path from s to t in G?
¤ The s-t connectivity problem
¤ The maze-solving problem
¨ A:
¤ For small graphs, easy! (visual inspection)
n 1-6 connectivity? 7-13 connectivity?
¤ What if large graphs? How efficiently can we do?
Graphs
18
1
2 3
4 5
6
7
8
9
10
11
12
13
room
hallway
IRIS H.-R. JIANG
Breadth-First-Search (BFS)
Graphs
19
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
9
10
11
12
13
L0
L1
L2
L3
¨ Breadth-first search (BFS): propagate the waves
¤ Start at s and flood the graph with an expanding wave that grows
to visit all nodes that it can reach.
¤ Layer Li: i is the time that a node is reached.
n Layer L0 = {s}; layer L1 = all neighbors of L0.
n Layer Lj+1 = all nodes that do not belong to an earlier layer and
that are neighbors of Lj.
n i = distance between s to the nodes that belong to layer Li.
Adjacent nodes
IRIS H.-R. JIANG
BFS Tree
Graphs
20
L0
L1
L2
L3 1
2 3
4 5
6
7
8
Nontree edge
Tree edge
BFS tree
¨ Let T be a BFS tree, let x and y be nodes in T belonging to
layers Li and Lj respectively, and let (x, y) be an edge of G. Then
i and j differ by at most 1.
¨ Pf:
¤ Without loss of generality, suppose j – i > 1.
¤ By definition, xÎLi , x’s neighbors belongs to Li+1 or earlier.
¤ Since (x, y) is an edge of G, y is x’s neighbor, yÎ Lj and j ! i+1.
¤ ®¬
1
2 3
4 5
6
8
7
L0
L1
L2
L3
BFS tree
19. BFS
DFS
Graph Connectivity and Graph Traversal
17
Graphs
1
2 3
4 5
6
7
8
9
10
11
12
13
IRIS H.-R. JIANG
Node-to-Node Connectivity
¨ Q: Given a graph G = (V, E) and two particular nodes s and t, is
there a path from s to t in G?
¤ The s-t connectivity problem
¤ The maze-solving problem
¨ A:
¤ For small graphs, easy! (visual inspection)
n 1-6 connectivity? 7-13 connectivity?
¤ What if large graphs? How efficiently can we do?
Graphs
18
1
2 3
4 5
6
7
8
9
10
11
12
13
room
hallway
IRIS H.-R. JIANG
Breadth-First-Search (BFS)
Graphs
19
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
9
10
11
12
13
L0
L1
L2
L3
¨ Breadth-first search (BFS): propagate the waves
¤ Start at s and flood the graph with an expanding wave that grows
to visit all nodes that it can reach.
¤ Layer Li: i is the time that a node is reached.
n Layer L0 = {s}; layer L1 = all neighbors of L0.
n Layer Lj+1 = all nodes that do not belong to an earlier layer and
that are neighbors of Lj.
n i = distance between s to the nodes that belong to layer Li.
Adjacent nodes
IRIS H.-R. JIANG
BFS Tree
Graphs
20
L0
L1
L2
L3 1
2 3
4 5
6
7
8
Nontree edge
Tree edge
BFS tree
¨ Let T be a BFS tree, let x and y be nodes in T belonging to
layers Li and Lj respectively, and let (x, y) be an edge of G. Then
i and j differ by at most 1.
¨ Pf:
¤ Without loss of generality, suppose j – i > 1.
¤ By definition, xÎLi , x’s neighbors belongs to Li+1 or earlier.
¤ Since (x, y) is an edge of G, y is x’s neighbor, yÎ Lj and j ! i+1.
¤ ®¬
1
2 3
4 5
6
8
7
L0
L1
L2
L3
BFS tree
20. BFS
DFS
Graph Connectivity and Graph Traversal
17
Graphs
1
2 3
4 5
6
7
8
9
10
11
12
13
IRIS H.-R. JIANG
Node-to-Node Connectivity
¨ Q: Given a graph G = (V, E) and two particular nodes s and t, is
there a path from s to t in G?
¤ The s-t connectivity problem
¤ The maze-solving problem
¨ A:
¤ For small graphs, easy! (visual inspection)
n 1-6 connectivity? 7-13 connectivity?
¤ What if large graphs? How efficiently can we do?
Graphs
18
1
2 3
4 5
6
7
8
9
10
11
12
13
room
hallway
IRIS H.-R. JIANG
Breadth-First-Search (BFS)
Graphs
19
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
9
10
11
12
13
L0
L1
L2
L3
¨ Breadth-first search (BFS): propagate the waves
¤ Start at s and flood the graph with an expanding wave that grows
to visit all nodes that it can reach.
¤ Layer Li: i is the time that a node is reached.
n Layer L0 = {s}; layer L1 = all neighbors of L0.
n Layer Lj+1 = all nodes that do not belong to an earlier layer and
that are neighbors of Lj.
n i = distance between s to the nodes that belong to layer Li.
Adjacent nodes
IRIS H.-R. JIANG
BFS Tree
Graphs
20
L0
L1
L2
L3 1
2 3
4 5
6
7
8
Nontree edge
Tree edge
BFS tree
¨ Let T be a BFS tree, let x and y be nodes in T belonging to
layers Li and Lj respectively, and let (x, y) be an edge of G. Then
i and j differ by at most 1.
¨ Pf:
¤ Without loss of generality, suppose j – i > 1.
¤ By definition, xÎLi , x’s neighbors belongs to Li+1 or earlier.
¤ Since (x, y) is an edge of G, y is x’s neighbor, yÎ Lj and j ! i+1.
¤ ®¬
1
2 3
4 5
6
8
7
L0
L1
L2
L3
BFS tree
21. IRIS H.-R. JIANG
Connected Component
¨ A connected component containing s is the set of nodes that
are reachable from s.
¤ Connected component containing node 1 is {1, 2, 3, 4, 5, 6, 7, 8}.
¤ There are three connected components.
n The other two are {9, 10} and {11, 12, 13}.
Graphs
21
1
2 3
4 5
6
7
8
9
10
11
12
13
IRIS H.-R. JIANG
Color Fill
¨ Q: Given lime green pixel in an image, how to change color of
entire blob of neighboring lime pixels to blue?
¨ A: Model the image as a graph.
¤ Node: pixel.
¤ Edge: two neighboring lime pixels.
¤ Blob: connected component of lime pixels.
Graphs
22
recolor lime green blob to blue
IRIS H.-R. JIANG
Connected Component
¨ Find all nodes reachable from s:
¨ Correctness: Upon termination, R is the connected component
containing s.
¨ Pf:
¤ Q: How about any node vÎR?
¤ Q: How about a node wÏR?
¨ Q: How to recover the actual path from s to any node tÎR?
¨ Q: How to explore a new edge in line 2?
¤ BFS: explore in order of distance from s.
¤ Any method else?
Graphs
23
Connected-Component(s)
// R will consist of nodes to which s has a path
1. initialize R = {s}
2. while (there is an edge (u, v) where uÎR andvÏR) do
3. R = R + {v}
it's safe to add v
s
u
v
R
IRIS H.-R. JIANG
Depth-First Search (DFS)
¨ Depth-first search (DFS): Go as deeply as possible or retreat
¤ Start from s and try the first edge leading out, and so on, until
reach a dead end. Backtrack and repeat.
n A mouse in a maze without the map.
¤ Another method for finding connected component
Graphs
24
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
DFS(u)
1. mark u as explored and add u to R
2. foreach edge (u, v) incident to u do
3. if (v is not marked as explored) then
4. recursively invoke DFS(v)
22. IRIS H.-R. JIANG
Connected Component
¨ A connected component containing s is the set of nodes that
are reachable from s.
¤ Connected component containing node 1 is {1, 2, 3, 4, 5, 6, 7, 8}.
¤ There are three connected components.
n The other two are {9, 10} and {11, 12, 13}.
Graphs
21
1
2 3
4 5
6
7
8
9
10
11
12
13
IRIS H.-R. JIANG
Color Fill
¨ Q: Given lime green pixel in an image, how to change color of
entire blob of neighboring lime pixels to blue?
¨ A: Model the image as a graph.
¤ Node: pixel.
¤ Edge: two neighboring lime pixels.
¤ Blob: connected component of lime pixels.
Graphs
22
recolor lime green blob to blue
IRIS H.-R. JIANG
Connected Component
¨ Find all nodes reachable from s:
¨ Correctness: Upon termination, R is the connected component
containing s.
¨ Pf:
¤ Q: How about any node vÎR?
¤ Q: How about a node wÏR?
¨ Q: How to recover the actual path from s to any node tÎR?
¨ Q: How to explore a new edge in line 2?
¤ BFS: explore in order of distance from s.
¤ Any method else?
Graphs
23
Connected-Component(s)
// R will consist of nodes to which s has a path
1. initialize R = {s}
2. while (there is an edge (u, v) where uÎR andvÏR) do
3. R = R + {v}
it's safe to add v
s
u
v
R
IRIS H.-R. JIANG
Depth-First Search (DFS)
¨ Depth-first search (DFS): Go as deeply as possible or retreat
¤ Start from s and try the first edge leading out, and so on, until
reach a dead end. Backtrack and repeat.
n A mouse in a maze without the map.
¤ Another method for finding connected component
Graphs
24
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
DFS(u)
1. mark u as explored and add u to R
2. foreach edge (u, v) incident to u do
3. if (v is not marked as explored) then
4. recursively invoke DFS(v)
23. IRIS H.-R. JIANG
Connected Component
¨ A connected component containing s is the set of nodes that
are reachable from s.
¤ Connected component containing node 1 is {1, 2, 3, 4, 5, 6, 7, 8}.
¤ There are three connected components.
n The other two are {9, 10} and {11, 12, 13}.
Graphs
21
1
2 3
4 5
6
7
8
9
10
11
12
13
IRIS H.-R. JIANG
Color Fill
¨ Q: Given lime green pixel in an image, how to change color of
entire blob of neighboring lime pixels to blue?
¨ A: Model the image as a graph.
¤ Node: pixel.
¤ Edge: two neighboring lime pixels.
¤ Blob: connected component of lime pixels.
Graphs
22
recolor lime green blob to blue
IRIS H.-R. JIANG
Connected Component
¨ Find all nodes reachable from s:
¨ Correctness: Upon termination, R is the connected component
containing s.
¨ Pf:
¤ Q: How about any node vÎR?
¤ Q: How about a node wÏR?
¨ Q: How to recover the actual path from s to any node tÎR?
¨ Q: How to explore a new edge in line 2?
¤ BFS: explore in order of distance from s.
¤ Any method else?
Graphs
23
Connected-Component(s)
// R will consist of nodes to which s has a path
1. initialize R = {s}
2. while (there is an edge (u, v) where uÎR andvÏR) do
3. R = R + {v}
it's safe to add v
s
u
v
R
IRIS H.-R. JIANG
Depth-First Search (DFS)
¨ Depth-first search (DFS): Go as deeply as possible or retreat
¤ Start from s and try the first edge leading out, and so on, until
reach a dead end. Backtrack and repeat.
n A mouse in a maze without the map.
¤ Another method for finding connected component
Graphs
24
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
DFS(u)
1. mark u as explored and add u to R
2. foreach edge (u, v) incident to u do
3. if (v is not marked as explored) then
4. recursively invoke DFS(v)
24. IRIS H.-R. JIANG
Connected Component
¨ A connected component containing s is the set of nodes that
are reachable from s.
¤ Connected component containing node 1 is {1, 2, 3, 4, 5, 6, 7, 8}.
¤ There are three connected components.
n The other two are {9, 10} and {11, 12, 13}.
Graphs
21
1
2 3
4 5
6
7
8
9
10
11
12
13
IRIS H.-R. JIANG
Color Fill
¨ Q: Given lime green pixel in an image, how to change color of
entire blob of neighboring lime pixels to blue?
¨ A: Model the image as a graph.
¤ Node: pixel.
¤ Edge: two neighboring lime pixels.
¤ Blob: connected component of lime pixels.
Graphs
22
recolor lime green blob to blue
IRIS H.-R. JIANG
Connected Component
¨ Find all nodes reachable from s:
¨ Correctness: Upon termination, R is the connected component
containing s.
¨ Pf:
¤ Q: How about any node vÎR?
¤ Q: How about a node wÏR?
¨ Q: How to recover the actual path from s to any node tÎR?
¨ Q: How to explore a new edge in line 2?
¤ BFS: explore in order of distance from s.
¤ Any method else?
Graphs
23
Connected-Component(s)
// R will consist of nodes to which s has a path
1. initialize R = {s}
2. while (there is an edge (u, v) where uÎR andvÏR) do
3. R = R + {v}
it's safe to add v
s
u
v
R
IRIS H.-R. JIANG
Depth-First Search (DFS)
¨ Depth-first search (DFS): Go as deeply as possible or retreat
¤ Start from s and try the first edge leading out, and so on, until
reach a dead end. Backtrack and repeat.
n A mouse in a maze without the map.
¤ Another method for finding connected component
Graphs
24
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
DFS(u)
1. mark u as explored and add u to R
2. foreach edge (u, v) incident to u do
3. if (v is not marked as explored) then
4. recursively invoke DFS(v)
25. IRIS H.-R. JIANG
DFS Tree
¨ Let T be a DFS tree, let x and y be nodes in T, and let (x, y) be a
nontree edge. Then one of x or y is an ancestor of the other.
¨ Pf:
¤ WLOG, suppose x is reached first by DFS.
¤ When (x, y) is examined during DFS(x), it is not added to T
because y is marked explored.
¤ Since y is not marked as explored when DFS(x) was first
invoked, it is a node that was discovered between the invocation
and end of the recursive call DFS(x).
¤ y is a descendant of x.
Graphs
25
DFS tree
1
2
4
5
6
7
8
1
2 3
4 5
6
7
8
DFS tree
Nontree edge
Tree edge
descendant
IRIS H.-R. JIANG
Summary: BFS and DFS
¨ Similarity: BFS/DFS builds the connected component
containing s.
¨ Difference: BFS tree is flat/short; DFS tree is narrow/deep.
¤ What are the nontree edges in BFS/DFS?
¨ Q: How to produce all the connected components of a graph?
¨ A:
Graphs
26
1
2 3
4 5
6
7
8
9
10
11
12
13
Lists / arrays
Queues / stacks
Implementation
27
Graphs
1
2 3
4 5
6
7
8
9
10
11
12
13
IRIS H.-R. JIANG
Representing Graphs
¨ A graph G = (V, E)
¤ |V| = the number of nodes = n
¤ |E| = the number of edges = m
¨ Dense or sparse?
¤ For a connected graph, n – 1 ! m ! ! n2
¨ Linear time = O(m+n)
¤ Why? It takes O(m+n) to read the input
Graphs
28
1
2 3
4 5
6
7
8
cardinality (size) of a set
n
2
26. IRIS H.-R. JIANG
DFS Tree
¨ Let T be a DFS tree, let x and y be nodes in T, and let (x, y) be a
nontree edge. Then one of x or y is an ancestor of the other.
¨ Pf:
¤ WLOG, suppose x is reached first by DFS.
¤ When (x, y) is examined during DFS(x), it is not added to T
because y is marked explored.
¤ Since y is not marked as explored when DFS(x) was first
invoked, it is a node that was discovered between the invocation
and end of the recursive call DFS(x).
¤ y is a descendant of x.
Graphs
25
DFS tree
1
2
4
5
6
7
8
1
2 3
4 5
6
7
8
DFS tree
Nontree edge
Tree edge
descendant
IRIS H.-R. JIANG
Summary: BFS and DFS
¨ Similarity: BFS/DFS builds the connected component
containing s.
¨ Difference: BFS tree is flat/short; DFS tree is narrow/deep.
¤ What are the nontree edges in BFS/DFS?
¨ Q: How to produce all the connected components of a graph?
¨ A:
Graphs
26
1
2 3
4 5
6
7
8
9
10
11
12
13
Lists / arrays
Queues / stacks
Implementation
27
Graphs
1
2 3
4 5
6
7
8
9
10
11
12
13
IRIS H.-R. JIANG
Representing Graphs
¨ A graph G = (V, E)
¤ |V| = the number of nodes = n
¤ |E| = the number of edges = m
¨ Dense or sparse?
¤ For a connected graph, n – 1 ! m ! ! n2
¨ Linear time = O(m+n)
¤ Why? It takes O(m+n) to read the input
Graphs
28
1
2 3
4 5
6
7
8
cardinality (size) of a set
n
2
27. IRIS H.-R. JIANG
DFS Tree
¨ Let T be a DFS tree, let x and y be nodes in T, and let (x, y) be a
nontree edge. Then one of x or y is an ancestor of the other.
¨ Pf:
¤ WLOG, suppose x is reached first by DFS.
¤ When (x, y) is examined during DFS(x), it is not added to T
because y is marked explored.
¤ Since y is not marked as explored when DFS(x) was first
invoked, it is a node that was discovered between the invocation
and end of the recursive call DFS(x).
¤ y is a descendant of x.
Graphs
25
DFS tree
1
2
4
5
6
7
8
1
2 3
4 5
6
7
8
DFS tree
Nontree edge
Tree edge
descendant
IRIS H.-R. JIANG
Summary: BFS and DFS
¨ Similarity: BFS/DFS builds the connected component
containing s.
¨ Difference: BFS tree is flat/short; DFS tree is narrow/deep.
¤ What are the nontree edges in BFS/DFS?
¨ Q: How to produce all the connected components of a graph?
¨ A:
Graphs
26
1
2 3
4 5
6
7
8
9
10
11
12
13
Lists / arrays
Queues / stacks
Implementation
27
Graphs
1
2 3
4 5
6
7
8
9
10
11
12
13
IRIS H.-R. JIANG
Representing Graphs
¨ A graph G = (V, E)
¤ |V| = the number of nodes = n
¤ |E| = the number of edges = m
¨ Dense or sparse?
¤ For a connected graph, n – 1 ! m ! ! n2
¨ Linear time = O(m+n)
¤ Why? It takes O(m+n) to read the input
Graphs
28
1
2 3
4 5
6
7
8
cardinality (size) of a set
n
2
28. IRIS H.-R. JIANG
DFS Tree
¨ Let T be a DFS tree, let x and y be nodes in T, and let (x, y) be a
nontree edge. Then one of x or y is an ancestor of the other.
¨ Pf:
¤ WLOG, suppose x is reached first by DFS.
¤ When (x, y) is examined during DFS(x), it is not added to T
because y is marked explored.
¤ Since y is not marked as explored when DFS(x) was first
invoked, it is a node that was discovered between the invocation
and end of the recursive call DFS(x).
¤ y is a descendant of x.
Graphs
25
DFS tree
1
2
4
5
6
7
8
1
2 3
4 5
6
7
8
DFS tree
Nontree edge
Tree edge
descendant
IRIS H.-R. JIANG
Summary: BFS and DFS
¨ Similarity: BFS/DFS builds the connected component
containing s.
¨ Difference: BFS tree is flat/short; DFS tree is narrow/deep.
¤ What are the nontree edges in BFS/DFS?
¨ Q: How to produce all the connected components of a graph?
¨ A:
Graphs
26
1
2 3
4 5
6
7
8
9
10
11
12
13
Lists / arrays
Queues / stacks
Implementation
27
Graphs
1
2 3
4 5
6
7
8
9
10
11
12
13
IRIS H.-R. JIANG
Representing Graphs
¨ A graph G = (V, E)
¤ |V| = the number of nodes = n
¤ |E| = the number of edges = m
¨ Dense or sparse?
¤ For a connected graph, n – 1 ! m ! ! n2
¨ Linear time = O(m+n)
¤ Why? It takes O(m+n) to read the input
Graphs
28
1
2 3
4 5
6
7
8
cardinality (size) of a set
n
2
29. IRIS H.-R. JIANG
Adjacency Matrix
¨ Consider a graph G = (V, E) with n nodes, V = {1, …, n}.
¨ The adjacency matrix of G is an nxn martix A where
¤ A[u, v] = 1 if (u, v) Î E;
¤ A[u, v] = 0, otherwise.
¨ Time:
¤ Q(1) time for checking if (u, v) Î E.
¤ Q(n) time for finding out all neighbors of some u Î V.
n Visit many 0’s
¨ Space: Q(n2)
¤ What if sparse graphs?
Graphs
29
1
2 3
4 5
6
7
8
1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 0 0
2 1 0 1 1 1 0 0 0
3 1 1 0 0 1 0 1 1
4 0 1 0 1 1 0 0 0
5 0 1 1 1 0 1 0 0
6 0 0 0 0 1 0 0 0
7 0 0 1 0 0 0 0 1
8 0 0 1 0 0 0 1 0
symmetric
IRIS H.-R. JIANG
Adjacency List
¨ The adjacency list of G is an array Adj[]of n lists, one for each
node represents its neighbors
¤ Adj[u] = a linked list of {v | (u, v) ÎE}.
¨ Time:
¤ Q(deg(u)) time for checking one edge or all neighbors of a node.
¨ Space: O(n+m)
Graphs
30
2 0
3
[1]
1 3
[2]
1 2
[3] 5
2 0
5
[4]
2m
2 3
[5]
0
5
[6]
3 0
8
[7]
3 0
7
[8]
1
2 3
4 5
6
7
8
7 0
8
4 0
6
4 0
5
degree of u: number of neighbors
IRIS H.-R. JIANG
What is a Queue?
¨ A queue is a set of elements from which we extract elements in
first-in, first-out (FIFO) order.
¤ We select elements in the same order in which they were added.
Graphs
31
front rear
Q = (a1, ..., an)
front, rear
A
rear
A B
front rear
A B C
front
Push (A) Push (B) Push (C)
rear
A B C D
front
Push (D)
rear
B C D
front
Pop ()
rear
B C D E
front
Push (E)
a1
rear
front
Push
Pop a2 … an
Two open
ended container
IRIS H.-R. JIANG
What is a Stack?
¨ A stack is a set of elements from which we extract elements in
last-in, first-out (LIFO) order.
¤ Each time we select an element, we choose the one that was
added most recently.
Graphs
32
bottom top
S = (a1, ..., an)
B
A
D
C
B
A
C
B
A
D
C
B
A
E
D
C
B
A
top
top
top
top
top
top
A
Push (A) Push (B) Push (C) Push (D) Push (E) Pop( ) = E
a1
top
bottom
Push
Pop
a2 … an
One open ended
and one close
ended container
30. IRIS H.-R. JIANG
Adjacency Matrix
¨ Consider a graph G = (V, E) with n nodes, V = {1, …, n}.
¨ The adjacency matrix of G is an nxn martix A where
¤ A[u, v] = 1 if (u, v) Î E;
¤ A[u, v] = 0, otherwise.
¨ Time:
¤ Q(1) time for checking if (u, v) Î E.
¤ Q(n) time for finding out all neighbors of some u Î V.
n Visit many 0’s
¨ Space: Q(n2)
¤ What if sparse graphs?
Graphs
29
1
2 3
4 5
6
7
8
1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 0 0
2 1 0 1 1 1 0 0 0
3 1 1 0 0 1 0 1 1
4 0 1 0 1 1 0 0 0
5 0 1 1 1 0 1 0 0
6 0 0 0 0 1 0 0 0
7 0 0 1 0 0 0 0 1
8 0 0 1 0 0 0 1 0
symmetric
IRIS H.-R. JIANG
Adjacency List
¨ The adjacency list of G is an array Adj[]of n lists, one for each
node represents its neighbors
¤ Adj[u] = a linked list of {v | (u, v) ÎE}.
¨ Time:
¤ Q(deg(u)) time for checking one edge or all neighbors of a node.
¨ Space: O(n+m)
Graphs
30
2 0
3
[1]
1 3
[2]
1 2
[3] 5
2 0
5
[4]
2m
2 3
[5]
0
5
[6]
3 0
8
[7]
3 0
7
[8]
1
2 3
4 5
6
7
8
7 0
8
4 0
6
4 0
5
degree of u: number of neighbors
IRIS H.-R. JIANG
What is a Queue?
¨ A queue is a set of elements from which we extract elements in
first-in, first-out (FIFO) order.
¤ We select elements in the same order in which they were added.
Graphs
31
front rear
Q = (a1, ..., an)
front, rear
A
rear
A B
front rear
A B C
front
Push (A) Push (B) Push (C)
rear
A B C D
front
Push (D)
rear
B C D
front
Pop ()
rear
B C D E
front
Push (E)
a1
rear
front
Push
Pop a2 … an
Two open
ended container
IRIS H.-R. JIANG
What is a Stack?
¨ A stack is a set of elements from which we extract elements in
last-in, first-out (LIFO) order.
¤ Each time we select an element, we choose the one that was
added most recently.
Graphs
32
bottom top
S = (a1, ..., an)
B
A
D
C
B
A
C
B
A
D
C
B
A
E
D
C
B
A
top
top
top
top
top
top
A
Push (A) Push (B) Push (C) Push (D) Push (E) Pop( ) = E
a1
top
bottom
Push
Pop
a2 … an
One open ended
and one close
ended container
31. IRIS H.-R. JIANG
Adjacency Matrix
¨ Consider a graph G = (V, E) with n nodes, V = {1, …, n}.
¨ The adjacency matrix of G is an nxn martix A where
¤ A[u, v] = 1 if (u, v) Î E;
¤ A[u, v] = 0, otherwise.
¨ Time:
¤ Q(1) time for checking if (u, v) Î E.
¤ Q(n) time for finding out all neighbors of some u Î V.
n Visit many 0’s
¨ Space: Q(n2)
¤ What if sparse graphs?
Graphs
29
1
2 3
4 5
6
7
8
1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 0 0
2 1 0 1 1 1 0 0 0
3 1 1 0 0 1 0 1 1
4 0 1 0 1 1 0 0 0
5 0 1 1 1 0 1 0 0
6 0 0 0 0 1 0 0 0
7 0 0 1 0 0 0 0 1
8 0 0 1 0 0 0 1 0
symmetric
IRIS H.-R. JIANG
Adjacency List
¨ The adjacency list of G is an array Adj[]of n lists, one for each
node represents its neighbors
¤ Adj[u] = a linked list of {v | (u, v) ÎE}.
¨ Time:
¤ Q(deg(u)) time for checking one edge or all neighbors of a node.
¨ Space: O(n+m)
Graphs
30
2 0
3
[1]
1 3
[2]
1 2
[3] 5
2 0
5
[4]
2m
2 3
[5]
0
5
[6]
3 0
8
[7]
3 0
7
[8]
1
2 3
4 5
6
7
8
7 0
8
4 0
6
4 0
5
degree of u: number of neighbors
IRIS H.-R. JIANG
What is a Queue?
¨ A queue is a set of elements from which we extract elements in
first-in, first-out (FIFO) order.
¤ We select elements in the same order in which they were added.
Graphs
31
front rear
Q = (a1, ..., an)
front, rear
A
rear
A B
front rear
A B C
front
Push (A) Push (B) Push (C)
rear
A B C D
front
Push (D)
rear
B C D
front
Pop ()
rear
B C D E
front
Push (E)
a1
rear
front
Push
Pop a2 … an
Two open
ended container
IRIS H.-R. JIANG
What is a Stack?
¨ A stack is a set of elements from which we extract elements in
last-in, first-out (LIFO) order.
¤ Each time we select an element, we choose the one that was
added most recently.
Graphs
32
bottom top
S = (a1, ..., an)
B
A
D
C
B
A
C
B
A
D
C
B
A
E
D
C
B
A
top
top
top
top
top
top
A
Push (A) Push (B) Push (C) Push (D) Push (E) Pop( ) = E
a1
top
bottom
Push
Pop
a2 … an
One open ended
and one close
ended container
32. IRIS H.-R. JIANG
Adjacency Matrix
¨ Consider a graph G = (V, E) with n nodes, V = {1, …, n}.
¨ The adjacency matrix of G is an nxn martix A where
¤ A[u, v] = 1 if (u, v) Î E;
¤ A[u, v] = 0, otherwise.
¨ Time:
¤ Q(1) time for checking if (u, v) Î E.
¤ Q(n) time for finding out all neighbors of some u Î V.
n Visit many 0’s
¨ Space: Q(n2)
¤ What if sparse graphs?
Graphs
29
1
2 3
4 5
6
7
8
1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 0 0
2 1 0 1 1 1 0 0 0
3 1 1 0 0 1 0 1 1
4 0 1 0 1 1 0 0 0
5 0 1 1 1 0 1 0 0
6 0 0 0 0 1 0 0 0
7 0 0 1 0 0 0 0 1
8 0 0 1 0 0 0 1 0
symmetric
IRIS H.-R. JIANG
Adjacency List
¨ The adjacency list of G is an array Adj[]of n lists, one for each
node represents its neighbors
¤ Adj[u] = a linked list of {v | (u, v) ÎE}.
¨ Time:
¤ Q(deg(u)) time for checking one edge or all neighbors of a node.
¨ Space: O(n+m)
Graphs
30
2 0
3
[1]
1 3
[2]
1 2
[3] 5
2 0
5
[4]
2m
2 3
[5]
0
5
[6]
3 0
8
[7]
3 0
7
[8]
1
2 3
4 5
6
7
8
7 0
8
4 0
6
4 0
5
degree of u: number of neighbors
IRIS H.-R. JIANG
What is a Queue?
¨ A queue is a set of elements from which we extract elements in
first-in, first-out (FIFO) order.
¤ We select elements in the same order in which they were added.
Graphs
31
front rear
Q = (a1, ..., an)
front, rear
A
rear
A B
front rear
A B C
front
Push (A) Push (B) Push (C)
rear
A B C D
front
Push (D)
rear
B C D
front
Pop ()
rear
B C D E
front
Push (E)
a1
rear
front
Push
Pop a2 … an
Two open
ended container
IRIS H.-R. JIANG
What is a Stack?
¨ A stack is a set of elements from which we extract elements in
last-in, first-out (LIFO) order.
¤ Each time we select an element, we choose the one that was
added most recently.
Graphs
32
bottom top
S = (a1, ..., an)
B
A
D
C
B
A
C
B
A
D
C
B
A
E
D
C
B
A
top
top
top
top
top
top
A
Push (A) Push (B) Push (C) Push (D) Push (E) Pop( ) = E
a1
top
bottom
Push
Pop
a2 … an
One open ended
and one close
ended container
33. IRIS H.-R. JIANG
Linked Stacks and Queues
¨ Implement queues and stacks by linked lists
Graphs
33
datalink
top
.
.
.
0
Linked stack
Advantage?
- Variable sizes
- Insert/delete in O(1)
data link
front
Linked queue
0
rear
…
Push
Pop
Push
Pop
IRIS H.-R. JIANG
¨ Adjacency list is ideal for implementing BFS
¨ Q: How to manage each layer list L[i]?
¨ A: A queue/stack is fine
¤ Nodes in L[i] can be in any order
¤ Or, we can merge all layer lists into a single list L as a queue
BFS(s) // T will be BFS tree rooted at s; layer counter i; layer list L[i]
1. Discovered[s] = true; Discovered[v] = false for other v
2. i = 0; L[0] = {s}; T = {};
3. while (L[i] is not empty) do
4. L[i+1] = {};
5. for each (node uÎL[i]) do
6. for each (edge (u, v) incident to u) do
7. if (Discovered[v] = false) then
8. Discovered[v] = true
9. T = T + {(u, v)}
10. L[i+1] = L[i+1] + {v}
11. i++
Implementing BFS
Graphs
34
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
L0
L1
L2
L3
Q: Running time?
O(n2) or O(n+m)
Why?
IRIS H.-R. JIANG
¨ We implement DFS by adjacency list
¨ Recursive procedure
¨ Alternative implementation of DFS
¤ Process each adjacency list in the reverse order
DFS(u)
1. mark u as explored and add u to R
2. foreach edge (u, v) incident to u do
3. if (v is not marked as explored) then
4. recursively invoke DFS(v)
Implementing DFS
Graphs
35
DFS(s)
// S: a stack of nodes whose neighbors haven’t been entirely explored
1. S = {s}
2. while (S is not empty) do
3. remove a node u from S
4. if (Explored[u] = false) then
5. Explored[u] = true
6. for each (edge (u, v) incident to u) do
7. S = S + {v}
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
Q: Running time?
O(n2) or O(n+m)
Why?
IRIS H.-R. JIANG
Summary: Implementation
¨ Graph:
¨ Graph traversal
¤ BFS: queue (or stack)
¤ DFS: stack
¤ O(n+m) time
Graphs
36
Adjacency matrix vs. Adjacency list Winner
Faster to find an edge? Matrix
Faster to find degree? List
Faster to traverse the graph? List
Storage for sparse graph? List
Storage for dense graph? Matrix
Edge insertion or deletion? Matrix
Better for most applications? List
34. IRIS H.-R. JIANG
Linked Stacks and Queues
¨ Implement queues and stacks by linked lists
Graphs
33
datalink
top
.
.
.
0
Linked stack
Advantage?
- Variable sizes
- Insert/delete in O(1)
data link
front
Linked queue
0
rear
…
Push
Pop
Push
Pop
IRIS H.-R. JIANG
¨ Adjacency list is ideal for implementing BFS
¨ Q: How to manage each layer list L[i]?
¨ A: A queue/stack is fine
¤ Nodes in L[i] can be in any order
¤ Or, we can merge all layer lists into a single list L as a queue
BFS(s) // T will be BFS tree rooted at s; layer counter i; layer list L[i]
1. Discovered[s] = true; Discovered[v] = false for other v
2. i = 0; L[0] = {s}; T = {};
3. while (L[i] is not empty) do
4. L[i+1] = {};
5. for each (node uÎL[i]) do
6. for each (edge (u, v) incident to u) do
7. if (Discovered[v] = false) then
8. Discovered[v] = true
9. T = T + {(u, v)}
10. L[i+1] = L[i+1] + {v}
11. i++
Implementing BFS
Graphs
34
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
L0
L1
L2
L3
Q: Running time?
O(n2) or O(n+m)
Why?
IRIS H.-R. JIANG
¨ We implement DFS by adjacency list
¨ Recursive procedure
¨ Alternative implementation of DFS
¤ Process each adjacency list in the reverse order
DFS(u)
1. mark u as explored and add u to R
2. foreach edge (u, v) incident to u do
3. if (v is not marked as explored) then
4. recursively invoke DFS(v)
Implementing DFS
Graphs
35
DFS(s)
// S: a stack of nodes whose neighbors haven’t been entirely explored
1. S = {s}
2. while (S is not empty) do
3. remove a node u from S
4. if (Explored[u] = false) then
5. Explored[u] = true
6. for each (edge (u, v) incident to u) do
7. S = S + {v}
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
Q: Running time?
O(n2) or O(n+m)
Why?
IRIS H.-R. JIANG
Summary: Implementation
¨ Graph:
¨ Graph traversal
¤ BFS: queue (or stack)
¤ DFS: stack
¤ O(n+m) time
Graphs
36
Adjacency matrix vs. Adjacency list Winner
Faster to find an edge? Matrix
Faster to find degree? List
Faster to traverse the graph? List
Storage for sparse graph? List
Storage for dense graph? Matrix
Edge insertion or deletion? Matrix
Better for most applications? List
35. IRIS H.-R. JIANG
Linked Stacks and Queues
¨ Implement queues and stacks by linked lists
Graphs
33
datalink
top
.
.
.
0
Linked stack
Advantage?
- Variable sizes
- Insert/delete in O(1)
data link
front
Linked queue
0
rear
…
Push
Pop
Push
Pop
IRIS H.-R. JIANG
¨ Adjacency list is ideal for implementing BFS
¨ Q: How to manage each layer list L[i]?
¨ A: A queue/stack is fine
¤ Nodes in L[i] can be in any order
¤ Or, we can merge all layer lists into a single list L as a queue
BFS(s) // T will be BFS tree rooted at s; layer counter i; layer list L[i]
1. Discovered[s] = true; Discovered[v] = false for other v
2. i = 0; L[0] = {s}; T = {};
3. while (L[i] is not empty) do
4. L[i+1] = {};
5. for each (node uÎL[i]) do
6. for each (edge (u, v) incident to u) do
7. if (Discovered[v] = false) then
8. Discovered[v] = true
9. T = T + {(u, v)}
10. L[i+1] = L[i+1] + {v}
11. i++
Implementing BFS
Graphs
34
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
L0
L1
L2
L3
Q: Running time?
O(n2) or O(n+m)
Why?
IRIS H.-R. JIANG
¨ We implement DFS by adjacency list
¨ Recursive procedure
¨ Alternative implementation of DFS
¤ Process each adjacency list in the reverse order
DFS(u)
1. mark u as explored and add u to R
2. foreach edge (u, v) incident to u do
3. if (v is not marked as explored) then
4. recursively invoke DFS(v)
Implementing DFS
Graphs
35
DFS(s)
// S: a stack of nodes whose neighbors haven’t been entirely explored
1. S = {s}
2. while (S is not empty) do
3. remove a node u from S
4. if (Explored[u] = false) then
5. Explored[u] = true
6. for each (edge (u, v) incident to u) do
7. S = S + {v}
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
Q: Running time?
O(n2) or O(n+m)
Why?
IRIS H.-R. JIANG
Summary: Implementation
¨ Graph:
¨ Graph traversal
¤ BFS: queue (or stack)
¤ DFS: stack
¤ O(n+m) time
Graphs
36
Adjacency matrix vs. Adjacency list Winner
Faster to find an edge? Matrix
Faster to find degree? List
Faster to traverse the graph? List
Storage for sparse graph? List
Storage for dense graph? Matrix
Edge insertion or deletion? Matrix
Better for most applications? List
36. IRIS H.-R. JIANG
Linked Stacks and Queues
¨ Implement queues and stacks by linked lists
Graphs
33
datalink
top
.
.
.
0
Linked stack
Advantage?
- Variable sizes
- Insert/delete in O(1)
data link
front
Linked queue
0
rear
…
Push
Pop
Push
Pop
IRIS H.-R. JIANG
¨ Adjacency list is ideal for implementing BFS
¨ Q: How to manage each layer list L[i]?
¨ A: A queue/stack is fine
¤ Nodes in L[i] can be in any order
¤ Or, we can merge all layer lists into a single list L as a queue
BFS(s) // T will be BFS tree rooted at s; layer counter i; layer list L[i]
1. Discovered[s] = true; Discovered[v] = false for other v
2. i = 0; L[0] = {s}; T = {};
3. while (L[i] is not empty) do
4. L[i+1] = {};
5. for each (node uÎL[i]) do
6. for each (edge (u, v) incident to u) do
7. if (Discovered[v] = false) then
8. Discovered[v] = true
9. T = T + {(u, v)}
10. L[i+1] = L[i+1] + {v}
11. i++
Implementing BFS
Graphs
34
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
L0
L1
L2
L3
Q: Running time?
O(n2) or O(n+m)
Why?
IRIS H.-R. JIANG
¨ We implement DFS by adjacency list
¨ Recursive procedure
¨ Alternative implementation of DFS
¤ Process each adjacency list in the reverse order
DFS(u)
1. mark u as explored and add u to R
2. foreach edge (u, v) incident to u do
3. if (v is not marked as explored) then
4. recursively invoke DFS(v)
Implementing DFS
Graphs
35
DFS(s)
// S: a stack of nodes whose neighbors haven’t been entirely explored
1. S = {s}
2. while (S is not empty) do
3. remove a node u from S
4. if (Explored[u] = false) then
5. Explored[u] = true
6. for each (edge (u, v) incident to u) do
7. S = S + {v}
1
2 3
4 5
6
7
8
1
2 3
4 5
6
7
8
Q: Running time?
O(n2) or O(n+m)
Why?
IRIS H.-R. JIANG
Summary: Implementation
¨ Graph:
¨ Graph traversal
¤ BFS: queue (or stack)
¤ DFS: stack
¤ O(n+m) time
Graphs
36
Adjacency matrix vs. Adjacency list Winner
Faster to find an edge? Matrix
Faster to find degree? List
Faster to traverse the graph? List
Storage for sparse graph? List
Storage for dense graph? Matrix
Edge insertion or deletion? Matrix
Better for most applications? List
37. Application of BFS
Testing Bipartiteness
37
Graphs
IRIS H.-R. JIANG
Bipartite Graphs
¨ A bipartite graph (bigraph) is a graph whose nodes can be
partitioned into sets X and Y in such a way that every edge has
one one end in X and the other end in Y.
¤ X and Y are two disjoint sets.
¤ No two nodes within the same set are adjacent.
Graphs
38
http://mathworld.wolfram.com/BipartiteGraph.html
X Y
G = (X, Y, E)
IRIS H.-R. JIANG
Is a Graph Bipartite?
¨ Q: Given a graph G, is it bipartite?
¨ A: Color the nodes with blue and red (two-coloring)
¨ If a graph G is bipartite, then it cannot contain an odd cycle.
Graphs
39
2 3
1
5
7
6 4
2 3
1
5
7
6
4
2
3
1
2
3
1
IRIS H.-R. JIANG
Testing Bipartiteness
¨ Color the nodes with blue and red
¨ Procedure:
1. Assume G = (V, E) is connected.
n Otherwise, we analyze connected components separately.
2. Pick any node sÎV and color it red
n Anyway, s must receive some color.
3. Color all the neighbors of s blue.
4. Repeat coloring red/blue until the whole graph is colored.
5. Test bipartiteness: every edge has ends of opposite colors.
Graphs
40
2 3
1
5
7
6 4
2 3
1
5
7
6
4
X Y
2 3
1
5
7
3
1
4
Arbitrarily
chosen
2
5
7
6
6
38. Application of BFS
Testing Bipartiteness
37
Graphs
IRIS H.-R. JIANG
Bipartite Graphs
¨ A bipartite graph (bigraph) is a graph whose nodes can be
partitioned into sets X and Y in such a way that every edge has
one one end in X and the other end in Y.
¤ X and Y are two disjoint sets.
¤ No two nodes within the same set are adjacent.
Graphs
38
http://mathworld.wolfram.com/BipartiteGraph.html
X Y
G = (X, Y, E)
IRIS H.-R. JIANG
Is a Graph Bipartite?
¨ Q: Given a graph G, is it bipartite?
¨ A: Color the nodes with blue and red (two-coloring)
¨ If a graph G is bipartite, then it cannot contain an odd cycle.
Graphs
39
2 3
1
5
7
6 4
2 3
1
5
7
6
4
2
3
1
2
3
1
IRIS H.-R. JIANG
Testing Bipartiteness
¨ Color the nodes with blue and red
¨ Procedure:
1. Assume G = (V, E) is connected.
n Otherwise, we analyze connected components separately.
2. Pick any node sÎV and color it red
n Anyway, s must receive some color.
3. Color all the neighbors of s blue.
4. Repeat coloring red/blue until the whole graph is colored.
5. Test bipartiteness: every edge has ends of opposite colors.
Graphs
40
2 3
1
5
7
6 4
2 3
1
5
7
6
4
X Y
2 3
1
5
7
3
1
4
Arbitrarily
chosen
2
5
7
6
6
39. Application of BFS
Testing Bipartiteness
37
Graphs
IRIS H.-R. JIANG
Bipartite Graphs
¨ A bipartite graph (bigraph) is a graph whose nodes can be
partitioned into sets X and Y in such a way that every edge has
one one end in X and the other end in Y.
¤ X and Y are two disjoint sets.
¤ No two nodes within the same set are adjacent.
Graphs
38
http://mathworld.wolfram.com/BipartiteGraph.html
X Y
G = (X, Y, E)
IRIS H.-R. JIANG
Is a Graph Bipartite?
¨ Q: Given a graph G, is it bipartite?
¨ A: Color the nodes with blue and red (two-coloring)
¨ If a graph G is bipartite, then it cannot contain an odd cycle.
Graphs
39
2 3
1
5
7
6 4
2 3
1
5
7
6
4
2
3
1
2
3
1
IRIS H.-R. JIANG
Testing Bipartiteness
¨ Color the nodes with blue and red
¨ Procedure:
1. Assume G = (V, E) is connected.
n Otherwise, we analyze connected components separately.
2. Pick any node sÎV and color it red
n Anyway, s must receive some color.
3. Color all the neighbors of s blue.
4. Repeat coloring red/blue until the whole graph is colored.
5. Test bipartiteness: every edge has ends of opposite colors.
Graphs
40
2 3
1
5
7
6 4
2 3
1
5
7
6
4
X Y
2 3
1
5
7
3
1
4
Arbitrarily
chosen
2
5
7
6
6
40. Application of BFS
Testing Bipartiteness
37
Graphs
IRIS H.-R. JIANG
Bipartite Graphs
¨ A bipartite graph (bigraph) is a graph whose nodes can be
partitioned into sets X and Y in such a way that every edge has
one one end in X and the other end in Y.
¤ X and Y are two disjoint sets.
¤ No two nodes within the same set are adjacent.
Graphs
38
http://mathworld.wolfram.com/BipartiteGraph.html
X Y
G = (X, Y, E)
IRIS H.-R. JIANG
Is a Graph Bipartite?
¨ Q: Given a graph G, is it bipartite?
¨ A: Color the nodes with blue and red (two-coloring)
¨ If a graph G is bipartite, then it cannot contain an odd cycle.
Graphs
39
2 3
1
5
7
6 4
2 3
1
5
7
6
4
2
3
1
2
3
1
IRIS H.-R. JIANG
Testing Bipartiteness
¨ Color the nodes with blue and red
¨ Procedure:
1. Assume G = (V, E) is connected.
n Otherwise, we analyze connected components separately.
2. Pick any node sÎV and color it red
n Anyway, s must receive some color.
3. Color all the neighbors of s blue.
4. Repeat coloring red/blue until the whole graph is colored.
5. Test bipartiteness: every edge has ends of opposite colors.
Graphs
40
2 3
1
5
7
6 4
2 3
1
5
7
6
4
X Y
2 3
1
5
7
3
1
4
Arbitrarily
chosen
2
5
7
6
6
41. IRIS H.-R. JIANG
Implementation: Testing Bipartiteness
¨ Q: How to implement this procedure?
¨ A: BFS! O(m+n)-time
¤ We perform BFS from any s, coloring s red, all of layer L1 blue, …
n Even/odd-numbered layers red/blue
n Insert the following statements after line 10 of BFS(s) (p. 34)
Graphs
41
2 3
1
5
7
6 4
2 3
1
5
7
6
4
X Y
Arbitrarily
chosen
2 3
1
5
7
6 4
10. L[i+1] = L[i+1] + {v}
10a. if ((i+1) is even) then
10b. Color[v] = red
10c. else
10d. Color[v] = blue
IRIS H.-R. JIANG
Proof: Correctness (1/2)
¨ Let G be a connected graph and let L0, L1, … be the layers
produced by BFS starting at node s. Then exactly one of the
following holds.
1. No edge of G joins two nodes of the same layer, and G is
bipartite.
2. An edge of G joins two nodes of the same layer, and G contains
an odd-length cycle (and hence is not bipartite).
¨ Pf: Case 1 is trivial.
Graphs
42
Let xÎLi, yÎLj, and (x, y) ÎE.
Then i and j differ by at most 1.
L0 L1 L2 L3 L0 L1 L2 L3
IRIS H.-R. JIANG
Proof: Correctness (2/2)
¨ Pf: (Case 2)
¤ Suppose (x, y) is an edge with x, y in same layer Lj.
¤ Let z = lca(x, y) = lowest common ancestor.
¤ Let Li be the layer containing z.
¤ Consider the cycle that takes edge from x to y, then path from y
to z, then path from z to x.
¤ Its length is 1 + (j-i) + (j-i), which is odd.
Graphs
43
Let xÎLi, yÎLj, and (x, y) ÎE.
Then i and j differ by at most 1.
z
s
x
y
L0 Li Lj
(x, y) y ->z z ->x
z = lca(x, y)
Connectivity in Directed Graphs
44
Graphs
2 3
1
5
7
6 4
42. IRIS H.-R. JIANG
Implementation: Testing Bipartiteness
¨ Q: How to implement this procedure?
¨ A: BFS! O(m+n)-time
¤ We perform BFS from any s, coloring s red, all of layer L1 blue, …
n Even/odd-numbered layers red/blue
n Insert the following statements after line 10 of BFS(s) (p. 34)
Graphs
41
2 3
1
5
7
6 4
2 3
1
5
7
6
4
X Y
Arbitrarily
chosen
2 3
1
5
7
6 4
10. L[i+1] = L[i+1] + {v}
10a. if ((i+1) is even) then
10b. Color[v] = red
10c. else
10d. Color[v] = blue
IRIS H.-R. JIANG
Proof: Correctness (1/2)
¨ Let G be a connected graph and let L0, L1, … be the layers
produced by BFS starting at node s. Then exactly one of the
following holds.
1. No edge of G joins two nodes of the same layer, and G is
bipartite.
2. An edge of G joins two nodes of the same layer, and G contains
an odd-length cycle (and hence is not bipartite).
¨ Pf: Case 1 is trivial.
Graphs
42
Let xÎLi, yÎLj, and (x, y) ÎE.
Then i and j differ by at most 1.
L0 L1 L2 L3 L0 L1 L2 L3
IRIS H.-R. JIANG
Proof: Correctness (2/2)
¨ Pf: (Case 2)
¤ Suppose (x, y) is an edge with x, y in same layer Lj.
¤ Let z = lca(x, y) = lowest common ancestor.
¤ Let Li be the layer containing z.
¤ Consider the cycle that takes edge from x to y, then path from y
to z, then path from z to x.
¤ Its length is 1 + (j-i) + (j-i), which is odd.
Graphs
43
Let xÎLi, yÎLj, and (x, y) ÎE.
Then i and j differ by at most 1.
z
s
x
y
L0 Li Lj
(x, y) y ->z z ->x
z = lca(x, y)
Connectivity in Directed Graphs
44
Graphs
2 3
1
5
7
6 4
43. IRIS H.-R. JIANG
Implementation: Testing Bipartiteness
¨ Q: How to implement this procedure?
¨ A: BFS! O(m+n)-time
¤ We perform BFS from any s, coloring s red, all of layer L1 blue, …
n Even/odd-numbered layers red/blue
n Insert the following statements after line 10 of BFS(s) (p. 34)
Graphs
41
2 3
1
5
7
6 4
2 3
1
5
7
6
4
X Y
Arbitrarily
chosen
2 3
1
5
7
6 4
10. L[i+1] = L[i+1] + {v}
10a. if ((i+1) is even) then
10b. Color[v] = red
10c. else
10d. Color[v] = blue
IRIS H.-R. JIANG
Proof: Correctness (1/2)
¨ Let G be a connected graph and let L0, L1, … be the layers
produced by BFS starting at node s. Then exactly one of the
following holds.
1. No edge of G joins two nodes of the same layer, and G is
bipartite.
2. An edge of G joins two nodes of the same layer, and G contains
an odd-length cycle (and hence is not bipartite).
¨ Pf: Case 1 is trivial.
Graphs
42
Let xÎLi, yÎLj, and (x, y) ÎE.
Then i and j differ by at most 1.
L0 L1 L2 L3 L0 L1 L2 L3
IRIS H.-R. JIANG
Proof: Correctness (2/2)
¨ Pf: (Case 2)
¤ Suppose (x, y) is an edge with x, y in same layer Lj.
¤ Let z = lca(x, y) = lowest common ancestor.
¤ Let Li be the layer containing z.
¤ Consider the cycle that takes edge from x to y, then path from y
to z, then path from z to x.
¤ Its length is 1 + (j-i) + (j-i), which is odd.
Graphs
43
Let xÎLi, yÎLj, and (x, y) ÎE.
Then i and j differ by at most 1.
z
s
x
y
L0 Li Lj
(x, y) y ->z z ->x
z = lca(x, y)
Connectivity in Directed Graphs
44
Graphs
2 3
1
5
7
6 4
44. IRIS H.-R. JIANG
Implementation: Testing Bipartiteness
¨ Q: How to implement this procedure?
¨ A: BFS! O(m+n)-time
¤ We perform BFS from any s, coloring s red, all of layer L1 blue, …
n Even/odd-numbered layers red/blue
n Insert the following statements after line 10 of BFS(s) (p. 34)
Graphs
41
2 3
1
5
7
6 4
2 3
1
5
7
6
4
X Y
Arbitrarily
chosen
2 3
1
5
7
6 4
10. L[i+1] = L[i+1] + {v}
10a. if ((i+1) is even) then
10b. Color[v] = red
10c. else
10d. Color[v] = blue
IRIS H.-R. JIANG
Proof: Correctness (1/2)
¨ Let G be a connected graph and let L0, L1, … be the layers
produced by BFS starting at node s. Then exactly one of the
following holds.
1. No edge of G joins two nodes of the same layer, and G is
bipartite.
2. An edge of G joins two nodes of the same layer, and G contains
an odd-length cycle (and hence is not bipartite).
¨ Pf: Case 1 is trivial.
Graphs
42
Let xÎLi, yÎLj, and (x, y) ÎE.
Then i and j differ by at most 1.
L0 L1 L2 L3 L0 L1 L2 L3
IRIS H.-R. JIANG
Proof: Correctness (2/2)
¨ Pf: (Case 2)
¤ Suppose (x, y) is an edge with x, y in same layer Lj.
¤ Let z = lca(x, y) = lowest common ancestor.
¤ Let Li be the layer containing z.
¤ Consider the cycle that takes edge from x to y, then path from y
to z, then path from z to x.
¤ Its length is 1 + (j-i) + (j-i), which is odd.
Graphs
43
Let xÎLi, yÎLj, and (x, y) ÎE.
Then i and j differ by at most 1.
z
s
x
y
L0 Li Lj
(x, y) y ->z z ->x
z = lca(x, y)
Connectivity in Directed Graphs
44
Graphs
2 3
1
5
7
6 4
45. IRIS H.-R. JIANG
Recap: Directed Graphs
¨ In a directed graph: asymmetric relationships
¤ Edges are directed, i.e., (u, v) != (v, u)
n e.g., u knows v (celebrity), while v doesn’t know u.
n Directionality is crucial.
¨ Representation: Adjacency list
¤ Each node is associated with two lists, instead of one in an
undirected graph.
n To which
n From which
¨ Graph search algorithms: BFS/DFS
¤ Almost the same as undirected graphs
n Again, directionality is crucial.
n Q: What can we reach from node 1?
n A:
Graphs
45
u v
tail head
(u, v)
2 3
1
5
7
6 4
1
5
7
4
6
IRIS H.-R. JIANG
Strong Connectivity
¨ Nodes u and v are mutually reachable if there is a path from u
to v and also a path from v to u.
¨ A directed graph is strongly connected if every pair of nodes
are mutually reachable.
¤ Q: When are there no mutually reachable nodes?
¨ Lemma: If u and v are mutually reachable, and v and w are
mutually reachable, then u and w are mutually reachable.
¤ Simple but important!
¨ Pf:
Graphs
46
v
u
v
u w
v v
u w
v
IRIS H.-R. JIANG
Testing Strong Connectivity
¨ Q: Can we determine if a graph is strongly connected in linear
time?
¨ A: Yes. How? Why?
¤ Time: O(m+n)
¤ Q: Correctness?
¤ Q: How to partition a directed graph into strong components?
Graphs
47
TestSC(G)
1. pick any node s in G
2. R = BFS(s, G)
3. Rrev = BFS(s, Grev)
4. if (R = V = Rrev) then return true else false
Grev: reverse the direction of every edge in G
(u, v) in Grev if (v, u)in G
u
s
v
R = V = Rrev
IRIS H.-R. JIANG
Strong Component
¨ The strong component containing s in a directed graph is the
maximal set of all v s.t. s and v are mutually reachable.
¤ a.k.a. strongly connected component
¨ Theorem: For any two nodes s and t in a directed graph, their
strong components are either identical or disjoint.
¤ Q: When are they identical? When are they disjoint?
¨ Pf:
¤ Identical if s and t are mutually reachable
n s -- v, s -- t, v -- t
¤ Disjoint if s and t are not mutually reachable
n Proof by contradiction
Graphs
48
46. IRIS H.-R. JIANG
Recap: Directed Graphs
¨ In a directed graph: asymmetric relationships
¤ Edges are directed, i.e., (u, v) != (v, u)
n e.g., u knows v (celebrity), while v doesn’t know u.
n Directionality is crucial.
¨ Representation: Adjacency list
¤ Each node is associated with two lists, instead of one in an
undirected graph.
n To which
n From which
¨ Graph search algorithms: BFS/DFS
¤ Almost the same as undirected graphs
n Again, directionality is crucial.
n Q: What can we reach from node 1?
n A:
Graphs
45
u v
tail head
(u, v)
2 3
1
5
7
6 4
1
5
7
4
6
IRIS H.-R. JIANG
Strong Connectivity
¨ Nodes u and v are mutually reachable if there is a path from u
to v and also a path from v to u.
¨ A directed graph is strongly connected if every pair of nodes
are mutually reachable.
¤ Q: When are there no mutually reachable nodes?
¨ Lemma: If u and v are mutually reachable, and v and w are
mutually reachable, then u and w are mutually reachable.
¤ Simple but important!
¨ Pf:
Graphs
46
v
u
v
u w
v v
u w
v
IRIS H.-R. JIANG
Testing Strong Connectivity
¨ Q: Can we determine if a graph is strongly connected in linear
time?
¨ A: Yes. How? Why?
¤ Time: O(m+n)
¤ Q: Correctness?
¤ Q: How to partition a directed graph into strong components?
Graphs
47
TestSC(G)
1. pick any node s in G
2. R = BFS(s, G)
3. Rrev = BFS(s, Grev)
4. if (R = V = Rrev) then return true else false
Grev: reverse the direction of every edge in G
(u, v) in Grev if (v, u)in G
u
s
v
R = V = Rrev
IRIS H.-R. JIANG
Strong Component
¨ The strong component containing s in a directed graph is the
maximal set of all v s.t. s and v are mutually reachable.
¤ a.k.a. strongly connected component
¨ Theorem: For any two nodes s and t in a directed graph, their
strong components are either identical or disjoint.
¤ Q: When are they identical? When are they disjoint?
¨ Pf:
¤ Identical if s and t are mutually reachable
n s -- v, s -- t, v -- t
¤ Disjoint if s and t are not mutually reachable
n Proof by contradiction
Graphs
48
47. IRIS H.-R. JIANG
Recap: Directed Graphs
¨ In a directed graph: asymmetric relationships
¤ Edges are directed, i.e., (u, v) != (v, u)
n e.g., u knows v (celebrity), while v doesn’t know u.
n Directionality is crucial.
¨ Representation: Adjacency list
¤ Each node is associated with two lists, instead of one in an
undirected graph.
n To which
n From which
¨ Graph search algorithms: BFS/DFS
¤ Almost the same as undirected graphs
n Again, directionality is crucial.
n Q: What can we reach from node 1?
n A:
Graphs
45
u v
tail head
(u, v)
2 3
1
5
7
6 4
1
5
7
4
6
IRIS H.-R. JIANG
Strong Connectivity
¨ Nodes u and v are mutually reachable if there is a path from u
to v and also a path from v to u.
¨ A directed graph is strongly connected if every pair of nodes
are mutually reachable.
¤ Q: When are there no mutually reachable nodes?
¨ Lemma: If u and v are mutually reachable, and v and w are
mutually reachable, then u and w are mutually reachable.
¤ Simple but important!
¨ Pf:
Graphs
46
v
u
v
u w
v v
u w
v
IRIS H.-R. JIANG
Testing Strong Connectivity
¨ Q: Can we determine if a graph is strongly connected in linear
time?
¨ A: Yes. How? Why?
¤ Time: O(m+n)
¤ Q: Correctness?
¤ Q: How to partition a directed graph into strong components?
Graphs
47
TestSC(G)
1. pick any node s in G
2. R = BFS(s, G)
3. Rrev = BFS(s, Grev)
4. if (R = V = Rrev) then return true else false
Grev: reverse the direction of every edge in G
(u, v) in Grev if (v, u)in G
u
s
v
R = V = Rrev
IRIS H.-R. JIANG
Strong Component
¨ The strong component containing s in a directed graph is the
maximal set of all v s.t. s and v are mutually reachable.
¤ a.k.a. strongly connected component
¨ Theorem: For any two nodes s and t in a directed graph, their
strong components are either identical or disjoint.
¤ Q: When are they identical? When are they disjoint?
¨ Pf:
¤ Identical if s and t are mutually reachable
n s -- v, s -- t, v -- t
¤ Disjoint if s and t are not mutually reachable
n Proof by contradiction
Graphs
48
48. IRIS H.-R. JIANG
Recap: Directed Graphs
¨ In a directed graph: asymmetric relationships
¤ Edges are directed, i.e., (u, v) != (v, u)
n e.g., u knows v (celebrity), while v doesn’t know u.
n Directionality is crucial.
¨ Representation: Adjacency list
¤ Each node is associated with two lists, instead of one in an
undirected graph.
n To which
n From which
¨ Graph search algorithms: BFS/DFS
¤ Almost the same as undirected graphs
n Again, directionality is crucial.
n Q: What can we reach from node 1?
n A:
Graphs
45
u v
tail head
(u, v)
2 3
1
5
7
6 4
1
5
7
4
6
IRIS H.-R. JIANG
Strong Connectivity
¨ Nodes u and v are mutually reachable if there is a path from u
to v and also a path from v to u.
¨ A directed graph is strongly connected if every pair of nodes
are mutually reachable.
¤ Q: When are there no mutually reachable nodes?
¨ Lemma: If u and v are mutually reachable, and v and w are
mutually reachable, then u and w are mutually reachable.
¤ Simple but important!
¨ Pf:
Graphs
46
v
u
v
u w
v v
u w
v
IRIS H.-R. JIANG
Testing Strong Connectivity
¨ Q: Can we determine if a graph is strongly connected in linear
time?
¨ A: Yes. How? Why?
¤ Time: O(m+n)
¤ Q: Correctness?
¤ Q: How to partition a directed graph into strong components?
Graphs
47
TestSC(G)
1. pick any node s in G
2. R = BFS(s, G)
3. Rrev = BFS(s, Grev)
4. if (R = V = Rrev) then return true else false
Grev: reverse the direction of every edge in G
(u, v) in Grev if (v, u)in G
u
s
v
R = V = Rrev
IRIS H.-R. JIANG
Strong Component
¨ The strong component containing s in a directed graph is the
maximal set of all v s.t. s and v are mutually reachable.
¤ a.k.a. strongly connected component
¨ Theorem: For any two nodes s and t in a directed graph, their
strong components are either identical or disjoint.
¤ Q: When are they identical? When are they disjoint?
¨ Pf:
¤ Identical if s and t are mutually reachable
n s -- v, s -- t, v -- t
¤ Disjoint if s and t are not mutually reachable
n Proof by contradiction
Graphs
48
49. DAGs and Topological Ordering
49
Graphs
2 3
1
5
7
6 4
IRIS H.-R. JIANG
Directed Acyclic Graphs
¨ Q: If an undirected graph has no cycles, then what’s it?
¨ A: A tree (or forest).
¤ At most n-1 edges.
¨ A directed acyclic graph (DAG) is a directed graph without
cycles.
¤ A DAG may have a rich structure.
¤ A DAG encodes dependency or precedence constraints
n e.g., prerequisite of Algorithms:
n Data structures
n Discrete math
n Programming C/C++
n e.g., execution order of instructions in CPU
n Pipeline structures
Graphs
50
2 3
1
5
7
6 4
IRIS H.-R. JIANG
Topological Ordering
¨ Q: 4 drivers come to the junction simultaneously, who goes
first?
¤ Deadlock! Dependencies form a cycle!
¨ Given a directed graph G, a topological ordering is an ordering
of its nodes as v1, v2, …, vn so that for every edge (vi, vj), we
have i<j.
¤ Precedence constraints: edge (vi, vj) means vi must precede vj.
¨ Lemma: If G has a topological ordering, then G is a DAG.
¨ Pf: Proof by contradiction!
¤ How? Consider a cycle, vi, …, vj, vi.
Graphs
51
The driver must come to a complete stop at a stop sign. Generally the driver who arrives and stops first
continues first. If two or three drivers in different directions stop simultaneously at a junction controlled
by stop signs, generally the drivers on the left must yield the right-of-way to the driver on the far right.
vj
vi
IRIS H.-R. JIANG
Example
¨ Example:
¨ Lemma: If G has a topological ordering, then G is a DAG.
¨ Q: If G is a DAG, then does G have a topological ordering?
¨ Q: If so, how do we compute one?
¨ A: Key: find a way to get started!
¤ Q: How?
Graphs
52
a DAG
2 3
1
5
7
6 4
the same DAG with topological ordering
2 3
1 5 7
6
4
50. DAGs and Topological Ordering
49
Graphs
2 3
1
5
7
6 4
IRIS H.-R. JIANG
Directed Acyclic Graphs
¨ Q: If an undirected graph has no cycles, then what’s it?
¨ A: A tree (or forest).
¤ At most n-1 edges.
¨ A directed acyclic graph (DAG) is a directed graph without
cycles.
¤ A DAG may have a rich structure.
¤ A DAG encodes dependency or precedence constraints
n e.g., prerequisite of Algorithms:
n Data structures
n Discrete math
n Programming C/C++
n e.g., execution order of instructions in CPU
n Pipeline structures
Graphs
50
2 3
1
5
7
6 4
IRIS H.-R. JIANG
Topological Ordering
¨ Q: 4 drivers come to the junction simultaneously, who goes
first?
¤ Deadlock! Dependencies form a cycle!
¨ Given a directed graph G, a topological ordering is an ordering
of its nodes as v1, v2, …, vn so that for every edge (vi, vj), we
have i<j.
¤ Precedence constraints: edge (vi, vj) means vi must precede vj.
¨ Lemma: If G has a topological ordering, then G is a DAG.
¨ Pf: Proof by contradiction!
¤ How? Consider a cycle, vi, …, vj, vi.
Graphs
51
The driver must come to a complete stop at a stop sign. Generally the driver who arrives and stops first
continues first. If two or three drivers in different directions stop simultaneously at a junction controlled
by stop signs, generally the drivers on the left must yield the right-of-way to the driver on the far right.
vj
vi
IRIS H.-R. JIANG
Example
¨ Example:
¨ Lemma: If G has a topological ordering, then G is a DAG.
¨ Q: If G is a DAG, then does G have a topological ordering?
¨ Q: If so, how do we compute one?
¨ A: Key: find a way to get started!
¤ Q: How?
Graphs
52
a DAG
2 3
1
5
7
6 4
the same DAG with topological ordering
2 3
1 5 7
6
4
51. DAGs and Topological Ordering
49
Graphs
2 3
1
5
7
6 4
IRIS H.-R. JIANG
Directed Acyclic Graphs
¨ Q: If an undirected graph has no cycles, then what’s it?
¨ A: A tree (or forest).
¤ At most n-1 edges.
¨ A directed acyclic graph (DAG) is a directed graph without
cycles.
¤ A DAG may have a rich structure.
¤ A DAG encodes dependency or precedence constraints
n e.g., prerequisite of Algorithms:
n Data structures
n Discrete math
n Programming C/C++
n e.g., execution order of instructions in CPU
n Pipeline structures
Graphs
50
2 3
1
5
7
6 4
IRIS H.-R. JIANG
Topological Ordering
¨ Q: 4 drivers come to the junction simultaneously, who goes
first?
¤ Deadlock! Dependencies form a cycle!
¨ Given a directed graph G, a topological ordering is an ordering
of its nodes as v1, v2, …, vn so that for every edge (vi, vj), we
have i<j.
¤ Precedence constraints: edge (vi, vj) means vi must precede vj.
¨ Lemma: If G has a topological ordering, then G is a DAG.
¨ Pf: Proof by contradiction!
¤ How? Consider a cycle, vi, …, vj, vi.
Graphs
51
The driver must come to a complete stop at a stop sign. Generally the driver who arrives and stops first
continues first. If two or three drivers in different directions stop simultaneously at a junction controlled
by stop signs, generally the drivers on the left must yield the right-of-way to the driver on the far right.
vj
vi
IRIS H.-R. JIANG
Example
¨ Example:
¨ Lemma: If G has a topological ordering, then G is a DAG.
¨ Q: If G is a DAG, then does G have a topological ordering?
¨ Q: If so, how do we compute one?
¨ A: Key: find a way to get started!
¤ Q: How?
Graphs
52
a DAG
2 3
1
5
7
6 4
the same DAG with topological ordering
2 3
1 5 7
6
4
52. DAGs and Topological Ordering
49
Graphs
2 3
1
5
7
6 4
IRIS H.-R. JIANG
Directed Acyclic Graphs
¨ Q: If an undirected graph has no cycles, then what’s it?
¨ A: A tree (or forest).
¤ At most n-1 edges.
¨ A directed acyclic graph (DAG) is a directed graph without
cycles.
¤ A DAG may have a rich structure.
¤ A DAG encodes dependency or precedence constraints
n e.g., prerequisite of Algorithms:
n Data structures
n Discrete math
n Programming C/C++
n e.g., execution order of instructions in CPU
n Pipeline structures
Graphs
50
2 3
1
5
7
6 4
IRIS H.-R. JIANG
Topological Ordering
¨ Q: 4 drivers come to the junction simultaneously, who goes
first?
¤ Deadlock! Dependencies form a cycle!
¨ Given a directed graph G, a topological ordering is an ordering
of its nodes as v1, v2, …, vn so that for every edge (vi, vj), we
have i<j.
¤ Precedence constraints: edge (vi, vj) means vi must precede vj.
¨ Lemma: If G has a topological ordering, then G is a DAG.
¨ Pf: Proof by contradiction!
¤ How? Consider a cycle, vi, …, vj, vi.
Graphs
51
The driver must come to a complete stop at a stop sign. Generally the driver who arrives and stops first
continues first. If two or three drivers in different directions stop simultaneously at a junction controlled
by stop signs, generally the drivers on the left must yield the right-of-way to the driver on the far right.
vj
vi
IRIS H.-R. JIANG
Example
¨ Example:
¨ Lemma: If G has a topological ordering, then G is a DAG.
¨ Q: If G is a DAG, then does G have a topological ordering?
¨ Q: If so, how do we compute one?
¨ A: Key: find a way to get started!
¤ Q: How?
Graphs
52
a DAG
2 3
1
5
7
6 4
the same DAG with topological ordering
2 3
1 5 7
6
4
53. IRIS H.-R. JIANG
Where to Start?
¨ A: A node that depends on no one, i.e., unconstrained.
¨ Lemma: In every DAG G, there is a node with no incoming
edges.
¨ Pf: Proof by contradiction!
¤ Suppose that G is a DAG where every node has at least one
incoming edge. Let's see how to find a cycle in G.
¤ Pick any node v, and begin following edges backward from v:
Since v has at least one incoming edge (u, v) we can walk
backward to u.
¤ Then, since u has at least one incoming edge (x, u), we can walk
backward to x; and so on.
¤ Repeat this process n+1 times (the initial v counts one). We will
visit some node w twice, since G has only n nodes.
¤ Let C denote the sequence of nodes encountered between
successive visits to w. Clearly, C is a cycle. ®¬
Graphs
53
w x v
u
w
IRIS H.-R. JIANG
2 3
1
5
7
6 4
Example: Topological Ordering
Graphs
54
1
2
1
2 3
1
2 3
1
4
2 3
1
5 4
2 3
1
5
6 4
2 3
1
5
7
6 4 2 3
1 5 7
6
4
IRIS H.-R. JIANG
Topological Ordering
¨ Lemma: If G is a DAG, then G has a topological ordering.
¨ Pf: Proof by induction!
1. Base case: true if n = 1.
2. Inductive step:
n Induction hypothesis: true for DAGs with up to n nodes
n Given a DAG on n+1 nodes, find a node v w/o incoming edges.
n G – {v} is a DAG, since deleting v cannot create any cycles.
n G – {v} has n nodes. By induction hypothesis, G – {v} has a
topological ordering.
n Place v first in topological ordering. This is safe since all edges
of v point forward.
n Then append nodes of G – {v} in topological order after v.
Graphs
55
G – {v}
v
v + <v1, v2, …, vn> = <v, v1, v2, …, vn>
IRIS H.-R. JIANG
A Linear-Time Algorithm
¨ In fact, the proof has already suggested an algorithm.
¨ Time: From O(n2) to O(m+n)
¤ O(n2)-time: Total n iterations; line 1 in O(n)-time. How?
¤ O(m+n)-time: How? Maintain the following information
n indeg(w) = # of incoming edges from undeleted nodes
n S = set of nodes without incoming edges from undeleted nodes
¤ Initialization: O(m+n) via single scan through graph
¤ Update: line 3 deletes v
n Remove v from S
n Decrement indeg(w) for all edges from v to w, and add w to S if
indeg(w) hits 0; this is O(1) per edge
Graphs
56
TopologicalOrder(G)
1. find a node v without incoming edges
2. order v
3. G = G – {v} // delete v from G
4. if (G is not empty) then TopologicalOrder(G)
54. IRIS H.-R. JIANG
Where to Start?
¨ A: A node that depends on no one, i.e., unconstrained.
¨ Lemma: In every DAG G, there is a node with no incoming
edges.
¨ Pf: Proof by contradiction!
¤ Suppose that G is a DAG where every node has at least one
incoming edge. Let's see how to find a cycle in G.
¤ Pick any node v, and begin following edges backward from v:
Since v has at least one incoming edge (u, v) we can walk
backward to u.
¤ Then, since u has at least one incoming edge (x, u), we can walk
backward to x; and so on.
¤ Repeat this process n+1 times (the initial v counts one). We will
visit some node w twice, since G has only n nodes.
¤ Let C denote the sequence of nodes encountered between
successive visits to w. Clearly, C is a cycle. ®¬
Graphs
53
w x v
u
w
IRIS H.-R. JIANG
2 3
1
5
7
6 4
Example: Topological Ordering
Graphs
54
1
2
1
2 3
1
2 3
1
4
2 3
1
5 4
2 3
1
5
6 4
2 3
1
5
7
6 4 2 3
1 5 7
6
4
IRIS H.-R. JIANG
Topological Ordering
¨ Lemma: If G is a DAG, then G has a topological ordering.
¨ Pf: Proof by induction!
1. Base case: true if n = 1.
2. Inductive step:
n Induction hypothesis: true for DAGs with up to n nodes
n Given a DAG on n+1 nodes, find a node v w/o incoming edges.
n G – {v} is a DAG, since deleting v cannot create any cycles.
n G – {v} has n nodes. By induction hypothesis, G – {v} has a
topological ordering.
n Place v first in topological ordering. This is safe since all edges
of v point forward.
n Then append nodes of G – {v} in topological order after v.
Graphs
55
G – {v}
v
v + <v1, v2, …, vn> = <v, v1, v2, …, vn>
IRIS H.-R. JIANG
A Linear-Time Algorithm
¨ In fact, the proof has already suggested an algorithm.
¨ Time: From O(n2) to O(m+n)
¤ O(n2)-time: Total n iterations; line 1 in O(n)-time. How?
¤ O(m+n)-time: How? Maintain the following information
n indeg(w) = # of incoming edges from undeleted nodes
n S = set of nodes without incoming edges from undeleted nodes
¤ Initialization: O(m+n) via single scan through graph
¤ Update: line 3 deletes v
n Remove v from S
n Decrement indeg(w) for all edges from v to w, and add w to S if
indeg(w) hits 0; this is O(1) per edge
Graphs
56
TopologicalOrder(G)
1. find a node v without incoming edges
2. order v
3. G = G – {v} // delete v from G
4. if (G is not empty) then TopologicalOrder(G)
55. IRIS H.-R. JIANG
Where to Start?
¨ A: A node that depends on no one, i.e., unconstrained.
¨ Lemma: In every DAG G, there is a node with no incoming
edges.
¨ Pf: Proof by contradiction!
¤ Suppose that G is a DAG where every node has at least one
incoming edge. Let's see how to find a cycle in G.
¤ Pick any node v, and begin following edges backward from v:
Since v has at least one incoming edge (u, v) we can walk
backward to u.
¤ Then, since u has at least one incoming edge (x, u), we can walk
backward to x; and so on.
¤ Repeat this process n+1 times (the initial v counts one). We will
visit some node w twice, since G has only n nodes.
¤ Let C denote the sequence of nodes encountered between
successive visits to w. Clearly, C is a cycle. ®¬
Graphs
53
w x v
u
w
IRIS H.-R. JIANG
2 3
1
5
7
6 4
Example: Topological Ordering
Graphs
54
1
2
1
2 3
1
2 3
1
4
2 3
1
5 4
2 3
1
5
6 4
2 3
1
5
7
6 4 2 3
1 5 7
6
4
IRIS H.-R. JIANG
Topological Ordering
¨ Lemma: If G is a DAG, then G has a topological ordering.
¨ Pf: Proof by induction!
1. Base case: true if n = 1.
2. Inductive step:
n Induction hypothesis: true for DAGs with up to n nodes
n Given a DAG on n+1 nodes, find a node v w/o incoming edges.
n G – {v} is a DAG, since deleting v cannot create any cycles.
n G – {v} has n nodes. By induction hypothesis, G – {v} has a
topological ordering.
n Place v first in topological ordering. This is safe since all edges
of v point forward.
n Then append nodes of G – {v} in topological order after v.
Graphs
55
G – {v}
v
v + <v1, v2, …, vn> = <v, v1, v2, …, vn>
IRIS H.-R. JIANG
A Linear-Time Algorithm
¨ In fact, the proof has already suggested an algorithm.
¨ Time: From O(n2) to O(m+n)
¤ O(n2)-time: Total n iterations; line 1 in O(n)-time. How?
¤ O(m+n)-time: How? Maintain the following information
n indeg(w) = # of incoming edges from undeleted nodes
n S = set of nodes without incoming edges from undeleted nodes
¤ Initialization: O(m+n) via single scan through graph
¤ Update: line 3 deletes v
n Remove v from S
n Decrement indeg(w) for all edges from v to w, and add w to S if
indeg(w) hits 0; this is O(1) per edge
Graphs
56
TopologicalOrder(G)
1. find a node v without incoming edges
2. order v
3. G = G – {v} // delete v from G
4. if (G is not empty) then TopologicalOrder(G)
56. IRIS H.-R. JIANG
Where to Start?
¨ A: A node that depends on no one, i.e., unconstrained.
¨ Lemma: In every DAG G, there is a node with no incoming
edges.
¨ Pf: Proof by contradiction!
¤ Suppose that G is a DAG where every node has at least one
incoming edge. Let's see how to find a cycle in G.
¤ Pick any node v, and begin following edges backward from v:
Since v has at least one incoming edge (u, v) we can walk
backward to u.
¤ Then, since u has at least one incoming edge (x, u), we can walk
backward to x; and so on.
¤ Repeat this process n+1 times (the initial v counts one). We will
visit some node w twice, since G has only n nodes.
¤ Let C denote the sequence of nodes encountered between
successive visits to w. Clearly, C is a cycle. ®¬
Graphs
53
w x v
u
w
IRIS H.-R. JIANG
2 3
1
5
7
6 4
Example: Topological Ordering
Graphs
54
1
2
1
2 3
1
2 3
1
4
2 3
1
5 4
2 3
1
5
6 4
2 3
1
5
7
6 4 2 3
1 5 7
6
4
IRIS H.-R. JIANG
Topological Ordering
¨ Lemma: If G is a DAG, then G has a topological ordering.
¨ Pf: Proof by induction!
1. Base case: true if n = 1.
2. Inductive step:
n Induction hypothesis: true for DAGs with up to n nodes
n Given a DAG on n+1 nodes, find a node v w/o incoming edges.
n G – {v} is a DAG, since deleting v cannot create any cycles.
n G – {v} has n nodes. By induction hypothesis, G – {v} has a
topological ordering.
n Place v first in topological ordering. This is safe since all edges
of v point forward.
n Then append nodes of G – {v} in topological order after v.
Graphs
55
G – {v}
v
v + <v1, v2, …, vn> = <v, v1, v2, …, vn>
IRIS H.-R. JIANG
A Linear-Time Algorithm
¨ In fact, the proof has already suggested an algorithm.
¨ Time: From O(n2) to O(m+n)
¤ O(n2)-time: Total n iterations; line 1 in O(n)-time. How?
¤ O(m+n)-time: How? Maintain the following information
n indeg(w) = # of incoming edges from undeleted nodes
n S = set of nodes without incoming edges from undeleted nodes
¤ Initialization: O(m+n) via single scan through graph
¤ Update: line 3 deletes v
n Remove v from S
n Decrement indeg(w) for all edges from v to w, and add w to S if
indeg(w) hits 0; this is O(1) per edge
Graphs
56
TopologicalOrder(G)
1. find a node v without incoming edges
2. order v
3. G = G – {v} // delete v from G
4. if (G is not empty) then TopologicalOrder(G)