The document discusses greedy algorithms and minimum spanning trees. It describes Prim's algorithm for finding a minimum spanning tree (MST) of a connected, undirected graph. Prim's algorithm uses a priority queue to maintain vertices, with the key of each vertex being the weight of its least-weight edge connecting it to already processed vertices. It iteratively extracts the minimum vertex from the queue until all vertices are processed, resulting in an MST. The time complexity of Prim's algorithm is O(ElogV) where V is the number of vertices and E is the number of edges.
The document discusses algorithms for finding minimum spanning trees in graphs. It describes two common algorithms: Kruskal's algorithm and Prim's algorithm. Kruskal's algorithm sorts the edges by weight and builds up the spanning tree by adding edges that do not create cycles. Prim's algorithm maintains a set of vertices in the spanning tree and iteratively adds the lowest weight edge connecting a vertex outside the set to one inside. Both run in O((V+E)logV) time using efficient data structures.
The document discusses algorithms for solving the single-source shortest path problem, including Dijkstra's algorithm, Bellman-Ford algorithm, and shortest paths in directed acyclic graphs (DAGs). It reviews the key ideas behind relaxation and how algorithms like Bellman-Ford and Dijkstra's use relaxation to iteratively improve the upper bound on shortest path distances. Running times of the algorithms are analyzed, with Dijkstra's algorithm taking O(ElogV) time and Bellman-Ford taking O(VE) time.
This document provides an introduction to graph theory. It defines key graph terminology like vertices, edges, directed and undirected graphs, paths, and connectivity. Examples are given to illustrate different graph types like trees, cycles, and complete graphs. Common graph representations like adjacency matrices and lists are also described. The document outlines theorems and properties of graphs, and discusses applications of graph theory concepts in areas like computer networks and chemistry.
The document discusses various graph theory concepts including:
- Types of graphs such as simple graphs, multigraphs, pseudographs, directed graphs, and directed multigraphs which differ based on allowed edge connections.
- Graph terminology including vertices, edges, degrees, adjacency, incidence, paths, cycles, and representations using adjacency lists and matrices.
- Weighted graphs and algorithms for finding shortest paths such as Dijkstra's algorithm.
- Euler and Hamilton paths/circuits and conditions for their existence.
- The traveling salesman problem of finding the shortest circuit visiting all vertices.
The document discusses various topics in graph theory including definitions, types, and applications of graphs. It covers definitions of graphs, edges, degrees, paths and connectivity. Graph representations like adjacency matrix and lists are presented. Different types of graphs are defined including simple, multigraphs, pseudographs, directed graphs. Special graph structures like trees, cycles, wheels and n-cubes are discussed along with theorems on handshaking and degrees in graphs. Bipartite graphs and graph isomorphism are also covered.
The document discusses minimum spanning trees (MSTs). It defines MSTs and provides examples of applications like wiring electronic circuits. It then describes two common algorithms for finding MSTs: Kruskal's algorithm and Prim's algorithm. Kruskal's algorithm finds MSTs by sorting edges by weight and adding edges that connect different components without creating cycles. Prim's algorithm grows an MST from a single vertex by always adding the lowest-weight edge connecting a vertex to the growing tree.
The document discusses algorithms and data structures covered in a CS algorithms course, including Dijkstra's algorithm, Bellman-Ford algorithm, shortest paths in DAGs, Kruskal's algorithm for minimum spanning trees, and disjoint-set union data structures. It provides examples and explanations of how each algorithm works.
The document discusses algorithms for finding minimum spanning trees in graphs. It describes two common algorithms: Kruskal's algorithm and Prim's algorithm. Kruskal's algorithm sorts the edges by weight and builds up the spanning tree by adding edges that do not create cycles. Prim's algorithm maintains a set of vertices in the spanning tree and iteratively adds the lowest weight edge connecting a vertex outside the set to one inside. Both run in O((V+E)logV) time using efficient data structures.
The document discusses algorithms for solving the single-source shortest path problem, including Dijkstra's algorithm, Bellman-Ford algorithm, and shortest paths in directed acyclic graphs (DAGs). It reviews the key ideas behind relaxation and how algorithms like Bellman-Ford and Dijkstra's use relaxation to iteratively improve the upper bound on shortest path distances. Running times of the algorithms are analyzed, with Dijkstra's algorithm taking O(ElogV) time and Bellman-Ford taking O(VE) time.
This document provides an introduction to graph theory. It defines key graph terminology like vertices, edges, directed and undirected graphs, paths, and connectivity. Examples are given to illustrate different graph types like trees, cycles, and complete graphs. Common graph representations like adjacency matrices and lists are also described. The document outlines theorems and properties of graphs, and discusses applications of graph theory concepts in areas like computer networks and chemistry.
The document discusses various graph theory concepts including:
- Types of graphs such as simple graphs, multigraphs, pseudographs, directed graphs, and directed multigraphs which differ based on allowed edge connections.
- Graph terminology including vertices, edges, degrees, adjacency, incidence, paths, cycles, and representations using adjacency lists and matrices.
- Weighted graphs and algorithms for finding shortest paths such as Dijkstra's algorithm.
- Euler and Hamilton paths/circuits and conditions for their existence.
- The traveling salesman problem of finding the shortest circuit visiting all vertices.
The document discusses various topics in graph theory including definitions, types, and applications of graphs. It covers definitions of graphs, edges, degrees, paths and connectivity. Graph representations like adjacency matrix and lists are presented. Different types of graphs are defined including simple, multigraphs, pseudographs, directed graphs. Special graph structures like trees, cycles, wheels and n-cubes are discussed along with theorems on handshaking and degrees in graphs. Bipartite graphs and graph isomorphism are also covered.
The document discusses minimum spanning trees (MSTs). It defines MSTs and provides examples of applications like wiring electronic circuits. It then describes two common algorithms for finding MSTs: Kruskal's algorithm and Prim's algorithm. Kruskal's algorithm finds MSTs by sorting edges by weight and adding edges that connect different components without creating cycles. Prim's algorithm grows an MST from a single vertex by always adding the lowest-weight edge connecting a vertex to the growing tree.
The document discusses algorithms and data structures covered in a CS algorithms course, including Dijkstra's algorithm, Bellman-Ford algorithm, shortest paths in DAGs, Kruskal's algorithm for minimum spanning trees, and disjoint-set union data structures. It provides examples and explanations of how each algorithm works.
Algorithm Design and Complexity - Course 9Traian Rebedea
The document describes Kruskal's algorithm for finding the minimum spanning tree (MST) of a connected, undirected graph. Kruskal's algorithm works by sorting the edges by weight and then greedily adding edges to the MST one by one, skipping any edges that would create cycles. It uses the property that a minimum edge between disjoint components of the partial MST must be part of the overall MST. The runtime is O(ElogE) where E is the number of edges. An example run on a graph is shown step-by-step to demonstrate the algorithm.
In mathematics, graph theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects.Graph theory is also important in real life.
The document discusses algorithms for finding the minimum spanning tree of a graph. It begins by defining what a spanning tree is - a subset of edges that connects all vertices using the fewest possible edges. It then explains Kruskal's and Prim's algorithms for finding the minimum spanning tree, which is the spanning tree with the lowest total edge weight. Kruskal's algorithm sorts the edges by weight and builds the tree by adding edges that do not create cycles. Prim's algorithm grows the tree from an initial vertex by always adding the lowest weight edge that connects to a new vertex. Pseudocode is provided for both algorithms.
The document discusses various graph data structures and algorithms. It defines directed and undirected graphs, and methods for representing graphs using adjacency matrices and adjacency lists. It then explains algorithms for finding shortest paths, spanning trees, minimum spanning trees, and solving the traveling salesman problem in graphs.
The document presents results on determining the metric dimension of t-fold wheel graphs. It defines t-fold wheel graphs and provides previous results on the metric dimension of other graphs. The main result is a theorem stating that for t-fold wheel graphs, the metric dimension is t+1 if the number of vertices n is 3, 4, or 5, and (n+t-2)/2 if n is greater than or equal to 6. The proof considers two cases based on the value of n and shows that sets of certain vertices are both resolving sets and cannot be made smaller.
The document discusses shortest path problems and algorithms. It defines the shortest path problem as finding the minimum weight path between two vertices in a weighted graph. It presents the Bellman-Ford algorithm, which can handle graphs with negative edge weights but detects negative cycles. It also presents Dijkstra's algorithm, which only works for graphs without negative edge weights. Key steps of the algorithms include initialization, relaxation of edges to update distance estimates, and ensuring the shortest path property is satisfied.
This document introduces some basic concepts in graph theory, including:
- A graph G is defined as a pair (V,E) where V is the set of vertices and E is the set of edges.
- Edges connect pairs of vertices and can be directed or undirected. Special types of edges include parallel edges and loops.
- Special graphs include simple graphs without parallel edges/loops, weighted graphs with numerical edge weights, and complete graphs where all vertex pairs are connected.
- Graphs can be represented by adjacency matrices and incidence matrices showing vertex-edge connections.
- Paths and cycles traverse vertices and edges, with Euler cycles passing through every edge once.
a graph search algorithm that solves the single-source shortest path problem for a graph with non-negative edge path costs, producing a shortest path tree. This algorithm is often used in routing and as a subroutine in other graph algorithms.
Graph theory concepts can be applied in various domains such as computer networks, chemistry, and transportation. Key graph theory concepts include:
- Graphs are represented by vertices (nodes) and edges, and can be directed or undirected. Common graph types include trees, cycles, and bipartite graphs.
- Graphs can be represented through matrices like the incidence matrix and adjacency matrix which describe the relationships between vertices and edges. Adjacency lists are another representation.
- Connectivity describes the reachability between vertices via paths along edges. A graph is connected if there is a path between all pairs of vertices.
- Other concepts include isomorphism, which determines if two graphs have the same structure, and
The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph. It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers.
Graph theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects. This document defines several key graph concepts, including types of graphs (directed, undirected, bipartite), graph terminology (vertices, edges, degree), theorems about graphs, and examples of special graphs (complete graphs, cycles, wheels, n-cubes, complete bipartite graphs). It provides examples of how graph theory is applied in fields like computer networks, chemistry, and scheduling.
This document provides an overview of graph theory. It defines various graph types including simple graphs, multigraphs, pseudographs, directed graphs, and labeled graphs. It also defines key graph terminology such as vertices, edges, degree, adjacency, connectivity, and planar graphs. Graph theory has many applications in fields like transportation, computer networks, and chemistry for modeling relationships between objects.
The document discusses weighted graphs and algorithms for finding minimum spanning trees and shortest paths in weighted graphs. It defines weighted graphs and describes the minimum spanning tree and shortest path problems. It then explains Prim's and Kruskal's algorithms for finding minimum spanning trees and Dijkstra's algorithm for finding shortest paths.
This document provides an overview of graph theory concepts. It defines what a graph is consisting of vertices and edges. It describes different types of graphs such as directed vs undirected, simple vs complex graphs. It introduces common graph terminology like degree of a vertex, adjacent/incident vertices, and connectivity. Examples of applications are given such as transportation networks, web graphs, and scheduling problems. Special graph cases like complete graphs and cycles are also defined.
This document discusses treewidth and algorithms for graphs of bounded treewidth. It begins by motivating treewidth as many NP-complete graph problems can be solved efficiently on special graph classes that are decomposable, like trees. It then introduces treewidth and defines it formally. The document discusses solving the maximum independent set problem on trees using dynamic programming. It then generalizes this approach to bounded treewidth graphs by introducing the concepts of t-boundaried graphs, and operations for introducing and forgetting vertices. It poses two open questions - how to find a good vertex ordering, and how to combine the tree and bounded treewidth approaches. Finally, it states that the answers to these questions will involve tree decompositions and treewidth.
Algorithm Design and Complexity - Course 10Traian Rebedea
The document provides an overview of algorithms for finding shortest paths in graphs. It discusses Dijkstra's algorithm and the Bellman-Ford algorithm for solving the single-source shortest paths (SSSP) problem. Dijkstra's algorithm uses a greedy approach and only works for graphs with non-negative edge weights, while Bellman-Ford can handle graphs with negative edge weights but requires more iterations to relax all edges. The document also covers properties like optimal substructure, triangle inequality, and initialization procedures that are common to SSSP algorithms.
The document discusses graph algorithms and graph theory concepts. It defines graphs, directed and undirected graphs, and graph terminology like vertices, edges, adjacency, paths, cycles, connectedness, and representations using adjacency lists and matrices. It also covers tree graphs and their properties.
This presentation summarizes key concepts about graphs. It defines a graph as a pair of sets (V,E) where V is the set of vertices and E is the set of edges. It discusses types of graphs like simple graphs and connected graphs. It also defines important graph concepts such as subgraphs, walks, Euler paths and circuits, and shortest path problems. The presentation was delivered to lecturers in the Computer Science department on the topic of graphs.
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...sameer shah
"Join us for STATATHON, a dynamic 2-day event dedicated to exploring statistical knowledge and its real-world applications. From theory to practice, participants engage in intensive learning sessions, workshops, and challenges, fostering a deeper understanding of statistical methodologies and their significance in various fields."
Algorithm Design and Complexity - Course 9Traian Rebedea
The document describes Kruskal's algorithm for finding the minimum spanning tree (MST) of a connected, undirected graph. Kruskal's algorithm works by sorting the edges by weight and then greedily adding edges to the MST one by one, skipping any edges that would create cycles. It uses the property that a minimum edge between disjoint components of the partial MST must be part of the overall MST. The runtime is O(ElogE) where E is the number of edges. An example run on a graph is shown step-by-step to demonstrate the algorithm.
In mathematics, graph theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects.Graph theory is also important in real life.
The document discusses algorithms for finding the minimum spanning tree of a graph. It begins by defining what a spanning tree is - a subset of edges that connects all vertices using the fewest possible edges. It then explains Kruskal's and Prim's algorithms for finding the minimum spanning tree, which is the spanning tree with the lowest total edge weight. Kruskal's algorithm sorts the edges by weight and builds the tree by adding edges that do not create cycles. Prim's algorithm grows the tree from an initial vertex by always adding the lowest weight edge that connects to a new vertex. Pseudocode is provided for both algorithms.
The document discusses various graph data structures and algorithms. It defines directed and undirected graphs, and methods for representing graphs using adjacency matrices and adjacency lists. It then explains algorithms for finding shortest paths, spanning trees, minimum spanning trees, and solving the traveling salesman problem in graphs.
The document presents results on determining the metric dimension of t-fold wheel graphs. It defines t-fold wheel graphs and provides previous results on the metric dimension of other graphs. The main result is a theorem stating that for t-fold wheel graphs, the metric dimension is t+1 if the number of vertices n is 3, 4, or 5, and (n+t-2)/2 if n is greater than or equal to 6. The proof considers two cases based on the value of n and shows that sets of certain vertices are both resolving sets and cannot be made smaller.
The document discusses shortest path problems and algorithms. It defines the shortest path problem as finding the minimum weight path between two vertices in a weighted graph. It presents the Bellman-Ford algorithm, which can handle graphs with negative edge weights but detects negative cycles. It also presents Dijkstra's algorithm, which only works for graphs without negative edge weights. Key steps of the algorithms include initialization, relaxation of edges to update distance estimates, and ensuring the shortest path property is satisfied.
This document introduces some basic concepts in graph theory, including:
- A graph G is defined as a pair (V,E) where V is the set of vertices and E is the set of edges.
- Edges connect pairs of vertices and can be directed or undirected. Special types of edges include parallel edges and loops.
- Special graphs include simple graphs without parallel edges/loops, weighted graphs with numerical edge weights, and complete graphs where all vertex pairs are connected.
- Graphs can be represented by adjacency matrices and incidence matrices showing vertex-edge connections.
- Paths and cycles traverse vertices and edges, with Euler cycles passing through every edge once.
a graph search algorithm that solves the single-source shortest path problem for a graph with non-negative edge path costs, producing a shortest path tree. This algorithm is often used in routing and as a subroutine in other graph algorithms.
Graph theory concepts can be applied in various domains such as computer networks, chemistry, and transportation. Key graph theory concepts include:
- Graphs are represented by vertices (nodes) and edges, and can be directed or undirected. Common graph types include trees, cycles, and bipartite graphs.
- Graphs can be represented through matrices like the incidence matrix and adjacency matrix which describe the relationships between vertices and edges. Adjacency lists are another representation.
- Connectivity describes the reachability between vertices via paths along edges. A graph is connected if there is a path between all pairs of vertices.
- Other concepts include isomorphism, which determines if two graphs have the same structure, and
The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph. It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers.
Graph theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects. This document defines several key graph concepts, including types of graphs (directed, undirected, bipartite), graph terminology (vertices, edges, degree), theorems about graphs, and examples of special graphs (complete graphs, cycles, wheels, n-cubes, complete bipartite graphs). It provides examples of how graph theory is applied in fields like computer networks, chemistry, and scheduling.
This document provides an overview of graph theory. It defines various graph types including simple graphs, multigraphs, pseudographs, directed graphs, and labeled graphs. It also defines key graph terminology such as vertices, edges, degree, adjacency, connectivity, and planar graphs. Graph theory has many applications in fields like transportation, computer networks, and chemistry for modeling relationships between objects.
The document discusses weighted graphs and algorithms for finding minimum spanning trees and shortest paths in weighted graphs. It defines weighted graphs and describes the minimum spanning tree and shortest path problems. It then explains Prim's and Kruskal's algorithms for finding minimum spanning trees and Dijkstra's algorithm for finding shortest paths.
This document provides an overview of graph theory concepts. It defines what a graph is consisting of vertices and edges. It describes different types of graphs such as directed vs undirected, simple vs complex graphs. It introduces common graph terminology like degree of a vertex, adjacent/incident vertices, and connectivity. Examples of applications are given such as transportation networks, web graphs, and scheduling problems. Special graph cases like complete graphs and cycles are also defined.
This document discusses treewidth and algorithms for graphs of bounded treewidth. It begins by motivating treewidth as many NP-complete graph problems can be solved efficiently on special graph classes that are decomposable, like trees. It then introduces treewidth and defines it formally. The document discusses solving the maximum independent set problem on trees using dynamic programming. It then generalizes this approach to bounded treewidth graphs by introducing the concepts of t-boundaried graphs, and operations for introducing and forgetting vertices. It poses two open questions - how to find a good vertex ordering, and how to combine the tree and bounded treewidth approaches. Finally, it states that the answers to these questions will involve tree decompositions and treewidth.
Algorithm Design and Complexity - Course 10Traian Rebedea
The document provides an overview of algorithms for finding shortest paths in graphs. It discusses Dijkstra's algorithm and the Bellman-Ford algorithm for solving the single-source shortest paths (SSSP) problem. Dijkstra's algorithm uses a greedy approach and only works for graphs with non-negative edge weights, while Bellman-Ford can handle graphs with negative edge weights but requires more iterations to relax all edges. The document also covers properties like optimal substructure, triangle inequality, and initialization procedures that are common to SSSP algorithms.
The document discusses graph algorithms and graph theory concepts. It defines graphs, directed and undirected graphs, and graph terminology like vertices, edges, adjacency, paths, cycles, connectedness, and representations using adjacency lists and matrices. It also covers tree graphs and their properties.
This presentation summarizes key concepts about graphs. It defines a graph as a pair of sets (V,E) where V is the set of vertices and E is the set of edges. It discusses types of graphs like simple graphs and connected graphs. It also defines important graph concepts such as subgraphs, walks, Euler paths and circuits, and shortest path problems. The presentation was delivered to lecturers in the Computer Science department on the topic of graphs.
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...sameer shah
"Join us for STATATHON, a dynamic 2-day event dedicated to exploring statistical knowledge and its real-world applications. From theory to practice, participants engage in intensive learning sessions, workshops, and challenges, fostering a deeper understanding of statistical methodologies and their significance in various fields."
State of Artificial intelligence Report 2023kuntobimo2016
Artificial intelligence (AI) is a multidisciplinary field of science and engineering whose goal is to create intelligent machines.
We believe that AI will be a force multiplier on technological progress in our increasingly digital, data-driven world. This is because everything around us today, ranging from culture to consumer products, is a product of intelligence.
The State of AI Report is now in its sixth year. Consider this report as a compilation of the most interesting things we’ve seen with a goal of triggering an informed conversation about the state of AI and its implication for the future.
We consider the following key dimensions in our report:
Research: Technology breakthroughs and their capabilities.
Industry: Areas of commercial application for AI and its business impact.
Politics: Regulation of AI, its economic implications and the evolving geopolitics of AI.
Safety: Identifying and mitigating catastrophic risks that highly-capable future AI systems could pose to us.
Predictions: What we believe will happen in the next 12 months and a 2022 performance review to keep us honest.
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Data and AI
Round table discussion of vector databases, unstructured data, ai, big data, real-time, robots and Milvus.
A lively discussion with NJ Gen AI Meetup Lead, Prasad and Procure.FYI's Co-Found
Analysis insight about a Flyball dog competition team's performanceroli9797
Insight of my analysis about a Flyball dog competition team's last year performance. Find more: https://github.com/rolandnagy-ds/flyball_race_analysis/tree/main
Learn SQL from basic queries to Advance queriesmanishkhaire30
Dive into the world of data analysis with our comprehensive guide on mastering SQL! This presentation offers a practical approach to learning SQL, focusing on real-world applications and hands-on practice. Whether you're a beginner or looking to sharpen your skills, this guide provides the tools you need to extract, analyze, and interpret data effectively.
Key Highlights:
Foundations of SQL: Understand the basics of SQL, including data retrieval, filtering, and aggregation.
Advanced Queries: Learn to craft complex queries to uncover deep insights from your data.
Data Trends and Patterns: Discover how to identify and interpret trends and patterns in your datasets.
Practical Examples: Follow step-by-step examples to apply SQL techniques in real-world scenarios.
Actionable Insights: Gain the skills to derive actionable insights that drive informed decision-making.
Join us on this journey to enhance your data analysis capabilities and unlock the full potential of SQL. Perfect for data enthusiasts, analysts, and anyone eager to harness the power of data!
#DataAnalysis #SQL #LearningSQL #DataInsights #DataScience #Analytics
End-to-end pipeline agility - Berlin Buzzwords 2024Lars Albertsson
We describe how we achieve high change agility in data engineering by eliminating the fear of breaking downstream data pipelines through end-to-end pipeline testing, and by using schema metaprogramming to safely eliminate boilerplate involved in changes that affect whole pipelines.
A quick poll on agility in changing pipelines from end to end indicated a huge span in capabilities. For the question "How long time does it take for all downstream pipelines to be adapted to an upstream change," the median response was 6 months, but some respondents could do it in less than a day. When quantitative data engineering differences between the best and worst are measured, the span is often 100x-1000x, sometimes even more.
A long time ago, we suffered at Spotify from fear of changing pipelines due to not knowing what the impact might be downstream. We made plans for a technical solution to test pipelines end-to-end to mitigate that fear, but the effort failed for cultural reasons. We eventually solved this challenge, but in a different context. In this presentation we will describe how we test full pipelines effectively by manipulating workflow orchestration, which enables us to make changes in pipelines without fear of breaking downstream.
Making schema changes that affect many jobs also involves a lot of toil and boilerplate. Using schema-on-read mitigates some of it, but has drawbacks since it makes it more difficult to detect errors early. We will describe how we have rejected this tradeoff by applying schema metaprogramming, eliminating boilerplate but keeping the protection of static typing, thereby further improving agility to quickly modify data pipelines without fear.
1. Design and Analyses of Algorithms
LECTURE 6
Greedy Algorithms (and
Graphs)
• Graph representation
• Minimum spanning trees
• Optimal substructure
• Greedy choice
• Prim’s greedy MST
algorithm
2. Graphs
Definition. A directed graph (digraph)
G = (V, E) is an ordered pair consisting of
• a set V of vertices (singular: vertex),
• a set E V V of edges.
In an undirected graph G = (V, E), the edge
set E consists of unordered pairs of vertices.
In either case, we have |E| = O(V2). Moreover,
if G is connected, then | E | | V | – 1.
3. Adjacency-matrix
representation
A[i, j] =
The adjacency matrix of a graph G = (V, E), where
V = {1, 2, …, n}, is the matrix A[1 . . n, 1 . . n]
given by
1 if (i, j) E,
0 if (i, j) E.
4. Adjacency-matrix
representation
A[i, j] =
The adjacency matrix of a graph G = (V, E), where
V = {1, 2, …, n}, is the matrix A[1 . . n, 1 . . n]
given by
1 if (i, j) E,
0 if (i, j) E.
2 1
3 4
A 1 2 3 4
1 0 1 1 0
2 0 0 1 0
3 0 0 0 0
4 0 0 1 0
(V 2)storage
dense
representation.
5. Adjacency-list representation
An adjacency list of a vertex v V is the list Adj[v]
of vertices adjacent to v.
Adj[1] = {2, 3}
Adj[2] = {3}
Adj[3] = {}
Adj[4] = {3}
2 1
3 4
6. Adjacency-list representation
An adjacency list of a vertex v V is the list Adj[v]
of vertices adjacent to v.
Adj[1] = {2, 3}
Adj[2] = {3}
Adj[3] = {}
Adj[4] = {3}
2 1
3 4
For undirected graphs, |Adj[v]| = degree(v).
For digraphs, |Adj[v]| = out-degree(v).
7. Adjacency-list representation
An adjacency list of a vertex v V is the list Adj[v]
of vertices adjacent to v.
Adj[1] = {2, 3}
Adj[2] = {3}
Adj[3] = {}
Adj[4] = {3}
2 1
3 4
For undirected graphs, |Adj[v]| = degree(v).
For digraphs, |Adj[v]| = out-degree(v).
Handshaking Lemma: vV = 2 |E| for undirected
graphs adjacency lists use (V + E) storage —
a sparse representation (for either type of graph).
8. Minimum spanning trees
Input: A connected, undirected graph G = (V, E)
with weight function w : E R.
• For simplicity, assume that all edge weights are
distinct.
9. Minimum spanning trees
w(T ) w(u,v).
(u,v)T
Input: A connected, undirected graph G = (V, E)
with weight function w : E R.
• For simplicity, assume that all edge weights are
distinct.
Output: A spanning tree T — a tree that connects
all vertices — of minimum weight:
15. Remove any edge (u, v) T. Then, T is partitioned
into two subtrees T1 and T2.
T1
T2
u
v
Optimal substructure
MST T:
(Other edges of G
are not shown.)
16. T1
T2
u
v
Optimal substructure
MST T:
(Other edges of G
are not shown.)
Remove any edge (u, v) T. Then, T is partitioned
into two subtrees T1 and T2.
Theorem. The subtree T1 is an MST of G1 = (V1, E1),
the subgraph of G induced by the vertices of T1:
V1 = vertices of T1,
E1 = {(x, y) E : x, y V1 }.
Similarly for T2.
17. Proof of optimal substructure
Proof. Cut and paste:
w(T) = w(u, v) + w(T1) + w(T2).
If T
1
were a lower-weight spanning tree than T1 for
G1, then T = {(u, v)} T
1
T2 would bea
lower-weight spanning tree than T for G.
18. Proof of optimal substructure
Proof. Cut and paste:
w(T) = w(u, v) + w(T1) + w(T2).
If T
1
were a lower-weight spanning tree than T1 for
G1, then T = {(u, v)} T
1
T2 would bea
lower-weight spanning tree than T for G.
Do we also have overlapping subproblems?
•Yes.
19. Proof of optimal substructure
Proof. Cut and paste:
w(T) = w(u, v) + w(T1) + w(T2).
If T
1
were a lower-weight spanning tree than T1 for
G1, then T = {(u, v)} T
1
T2 would bea
lower-weight spanning tree than T for G.
Do we also have overlapping subproblems?
•Yes.
Great, then dynamic programming may work!
•Yes, but MST exhibits another powerful property
which leads to an even more efficient algorithm.
21. Hallmark for “greedy”
algorithms
Greedy-choice property
A locally optimal choice
is globally optimal.
Theorem. Let T be the MST of G = (V, E),
and let A V. Suppose that (u, v) E is the
least-weight edge connecting A to V – A.
Then, (u, v) T.
22. Proof of theorem
A
V – A
u
(u, v) = least-weight edge
connecting A to V – A
Proof. Suppose (u, v) T. Cut and paste.
T: v
23. Proof of theorem
A
V – A
u
(u, v) = least-weight edge
connecting A to V – A
Proof. Suppose (u, v) T. Cut and paste.
T: v
Consider the unique simple path from u to v in T.
24. Proof of theorem
A
V – A
u
(u, v) = least-weight edge
connecting A to V – A
Proof. Suppose (u, v) T. Cut and paste.
T: v
Consider the unique simple path from u to v in T.
Swap (u, v) with the first edge on this path that
connects a vertex in A to a vertex in V – A.
25. Proof of theorem
Proof. Suppose (u, v) T. Cut and paste.
u
(u, v) = least-weight edge
connecting A to V – A
v
T
:
A
V – A
Consider the unique simple path from u to v in T.
Swap (u, v) with the first edge on this path that
connects a vertex in A to a vertex in V – A.
A lighter-weight spanning tree than T results.
26. Prim’s algorithm
IDEA: Maintain V – A as a priority queue Q. Key
each vertex in Q with the weight of the least-
weight edge connecting it to a vertex in A.
Q V
key[v] for all v V
key[s] 0 for some arbitrary s V
while Q
do u EXTRACT-MIN(Q)
for each v Adj[u]
do if v Q and w(u, v) < key[v]
⊳
DECREASE-KEY
then key[v] w(u, v)
[v] u
At the end, {(v, [v])} forms the MST.
27. Example of Prim’s algorithm
A
V – A
6 12
5
14
3
8
10
9
0
15
7
28. Example of Prim’s algorithm
A
V – A
6 12
5
14
3
8
10
9
0
15
7
29. Example of Prim’s algorithm
A
V – A
0
0
6 12
5
14
3
8
10
15
9
7
30. Example of Prim’s algorithm
A
V – A
0
0
6 12
5
14
3
8
10
15
9
7
31. Example of Prim’s algorithm
A
V – A
0
0
6 12
5
14
3
8
10
15
9
7
32. Example of Prim’s algorithm
A
V – A
0
0
6 12
5
14
3
8
10
15
9
7
33. Example of Prim’s algorithm
A
V – A
0
0
6 12
5
14
3
8
10
15
9
7
34. Example of Prim’s algorithm
A
V – A
0
0
6 12
5
14
3
8
10
15
9
7
35. Example of Prim’s algorithm
A
V – A
0
0
6 12
5
14
3
8
10
15
9
7
36. Example of Prim’s algorithm
A
V – A
0
0
6 12
5
14
3
8
10
15
9
7
37. Example of Prim’s algorithm
A
V – A
0
0
6 12
5
14
3
8
10
15
9
7
38. Example of Prim’s algorithm
A
V – A
0
0
6 12
5
14
3
8
10
15
9
7
39. Example of Prim’s algorithm
A
V – A
0
0
6 12
5
14
3
8
10
15
9
7
40. Analysis of Prim
Q V
key[v] for all v V
key[s] 0 for some arbitrary s V
while Q
do u EXTRACT-MIN(Q)
for each v Adj[u]
do if v Q and w(u, v) < key[v]
then key[v] w(u, v)
[v] u
41. Analysis of Prim
key[v] for all v V
key[s] 0 for some arbitrary s V
while Q
do u EXTRACT-MIN(Q)
for each v Adj[u]
do if v Q and w(u, v) < key[v]
then key[v] w(u, v)
[v] u
Q V
(V)
total
42. Analysis of Prim
key[v] for all v V
key[s] 0 for some arbitrary s V
while Q
do u EXTRACT-MIN(Q)
for each v Adj[u]
do if v Q and w(u, v) < key[v]
then key[v] w(u, v)
[v] u
Q V
(V)
total
|V|
times
43. Analysis of Prim
key[v] for all v V
key[s] 0 for some arbitrary s V
while Q
do u EXTRACT-MIN(Q)
for each v Adj[u]
do if v Q and w(u, v) < key[v]
then key[v] w(u, v)
[v] u
degree(u)
times
|V|
times
Q V
(V)
total
44. Analysis of Prim
Handshaking Lemma (E) implicit DECREASE-KEY’s.
key[v] for all v V
key[s] 0 for some arbitrary s V
while Q
do u EXTRACT-MIN(Q)
for each v Adj[u]
do if v Q and w(u, v) < key[v]
then key[v] w(u, v)
[v] u
degree(u)
times
|V|
times
Q V
(V)
total
45. Analysis of Prim
key[v] for all v V
key[s] 0 for some arbitrary s V
while Q
do u EXTRACT-MIN(Q)
for each v Adj[u]
do if v Q and w(u, v) < key[v]
then key[v] w(u, v)
[v] u
degree(u)
times
|V|
times
Q V
(V)
total
Handshaking Lemma (E) implicit DECREASE-KEY’s.
Time = (V)·TEXTRACT-MIN + (E)·TDECREASE-KEY
46. MST algorithms
Kruskal’s algorithm
• Uses the disjoint-set data structure.
• Running time = O(E lgV).
Best to date:
• Karger, Klein, and Tarjan [1993].
• Randomized algorithm.
• O(V + E) expected time.