0% found this document useful (0 votes)
4 views

Lecture 9 - Graph Algorithms and Spanning trees

The document provides an overview of graph algorithms, focusing on concepts such as graph types, paths, cycles, and connectedness. It introduces spanning trees and minimum-cost spanning trees, explaining algorithms like Kruskal's and Prim's for finding them. Additionally, it discusses practical applications of spanning trees in real-world scenarios, such as cable routing and maze construction.

Uploaded by

markkifunye159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 9 - Graph Algorithms and Spanning trees

The document provides an overview of graph algorithms, focusing on concepts such as graph types, paths, cycles, and connectedness. It introduces spanning trees and minimum-cost spanning trees, explaining algorithms like Kruskal's and Prim's for finding them. Additionally, it discusses practical applications of spanning trees in real-world scenarios, such as cable routing and maze construction.

Uploaded by

markkifunye159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Graph Algorithms and

Spanning trees
What’s a Graph?
• A bunch of vertices connected by edges.
vertex

2
• Why Graph Algorithms?
• They’re fun. 1 3
• They’re interesting.
4
• They have surprisingly many applications. edge
Graph Types
• A directed graph edges’ allow travel in one direction
• An undirected graph edges’ allow travel in either direction
Basic Concepts
• A graph is an ordered pair (V, E).
• V is the set of vertices. (You can think of them as integers 1, 2, …, n.)
• E is the set of edges.
• An edge is a pair of vertices: (u, v).
• Note:
• Since E is a set, there is at most one edge between two vertices.
• Edges can be labeled with a weight: 10
Concepts: Directedness
• In a directed graph, the edges are “one-way.”
• So an edge (u, v) means you can go from u to v, but not vice versa.
a self-loop

• In an undirected graph, there is no direction on the edges: you can


go either way. (Also, no self-loops.)
Concepts: Adjacency
• Two vertices are adjacent if there is an edge between them.
• For a directed graph, u is adjacent to v if there is an edge (v, u).
v v
u w u w

u is adjacent to v. u is adjacent to v.
v is adjacent to u and w. v is adjacent to w.
w is adjacent to v.
Concepts: Degree
• Undirected graph: The degree of a vertex is the number of edges
touching it. degree 4

• For a directed graph, the in-degree is the number of edges entering


the vertex, and the out-degree is the number leaving it.
• The degree is the in-degree + the out-degree.

in-degree 1, out-degree 2
Concepts: Path
• A path is a sequence of adjacent vertices.
• The length of a path is the number of
edges it contains. 2 Is there a path from 1 to 4?
• i.e. one less than the number of What is its length?
vertices. What about from 4 to 1?
1 3
• We write u  v if there is path from u
How many paths are there
to v. (The correct symbol, a wiggly from 2 to 3? From 2 to 2?
arrow, is not available in standard fonts.) 4 From 1 to 1?
We say v is reachable from u.
More Terminology
• Simple path: no repeated vertices a b

c bec

d e
• Cycle: simple path, except that the last vertex is the same as the first
vertex a b

acda
c

d e
Concepts: Cycle
• A cycle is a path of length at least 1 from a vertex to itself.
• A graph with no cycles is acyclic.
• A path with no cycles is a simple path.
2

1 3

4
• The path <2, 3, 4, 2> is a cycle.
Concepts: Connectedness

• An undirected graph is connected if there is a path


between any two vertices.

An unconnected graph with


three connected components.
Concepts: Trees
• A free tree is a connected, acyclic, undirected graph.
• To get a rooted tree (the kind we’ve used up until now), designate some
vertex as the root.
• If the graph is disconnected, it’s a forest.
• Facts about free trees:
• |E| = |V| -1
• Any two vertices are connected by exactly one path.
• Removing an edge disconnects the graph.
• Adding an edge results in a cycle.
Graph Traversals
• We want to travel to every node in the graph
• Traversals guarantee that we will get to each node exactly
once
• This can be used if we want;
• To search for information held in the nodes
• To distribute information to each node.
• Two ways:
• Depth-First Traversal
• Breadth-First Traversal
Depth-First Traversal

• We follow a path through the graph until we reach a dead end


• We then go back up until we reach a node with an edge to an
unvisited node
• We take this edge and again follow it until we reach a dead end
• This process continues until we back up to the starting node and
it has no edges to unvisited nodes
Breadth-First Traversal
• From the starting node, we follow all paths of length one
• Then we follow paths of length two that go to unvisited
nodes
• We continue increasing the length of the paths until there
are no unvisited nodes along any of the paths

15
Spanning trees
• Suppose you have a connected undirected graph
• Connected: every node is reachable from every other node
• Undirected: edges do not have an associated direction
• Then a spanning tree of the graph is a connected sub-graph in which there are no
cycles.
• A spanning tree for a connected, undirected graph G is a graph S consisting of the
nodes of G together with enough edges of G such that:
• S is connected, and
• S is acyclic.
Spanning trees

A connected, Four of the spanning trees of the graph


undirected graph
Finding a spanning tree

• To find a spanning tree of a graph,


• Pick an initial node and call it part of the spanning tree
• Do a search from the initial node:
• Each time you find a node that is not in the spanning tree, add to the
spanning tree both the new node and the edge you followed to get to
it
Finding a spanning tree

One possible result


An undirected of a BFS One possible result of a
graph starting from top DFS
starting from top
Minimizing costs
• Suppose you want to supply a set of houses (say, in a new
subdivision) with:
• electric power
• water
• sewage lines
• telephone lines
• To keep costs down, you could connect these houses with a
spanning tree (of, for example, power lines)
• However, the houses are not all equal distances apart
• To reduce costs even further, you could connect the houses with a
minimum-cost spanning tree
Finding spanning trees

• There are two basic algorithms for finding minimum-cost


spanning trees, and both are greedy algorithms
• Kruskal’s algorithm: Start with no nodes or edges in the
spanning tree, and repeatedly add the cheapest edge that
does not create a cycle
• Here, we consider the spanning tree to consist of edges only
Finding spanning trees

• Prim’s algorithm: Start with any one node in the spanning


tree, and repeatedly add the cheapest edge, and the node
it leads to, for which the node is not already in the
spanning tree.
• Here, we consider the spanning tree to consist of both nodes
and edges
Mazes
• Typically,
• Every location in a maze is reachable from the starting
location
• There is only one path from start to finish
• If the cells are “vertices” and the open doors
between cells are “edges,” this describes a spanning
tree
• Since there is exactly one path between any pair of
cells, any cells can be used as “start” and “finish”
• This describes a spanning tree
Mazes as spanning trees
• While not every maze is a
spanning tree, most can be
represented as such
• The nodes are “places” within
the maze
• There is exactly one cycle-free
path from any node to any other
node
Building a maze I
• This algorithm requires two sets of cells
• the set of cells already in the spanning tree, IN
• the set of cells adjacent to the cells in the spanning tree (but not in it
themselves), called the FRONTIER
• Start with all walls present
• Pick any cell and put it into IN (red)
• Put all adjacent cells, that aren’t in IN, into FRONTIER (blue)
Building a maze I
Building a maze II

• Repeatedly do the following:


• Remove any one cell C from FRONTIER and put it in IN
• Erase the wall between C and some one adjacent cell in IN
• Add to FRONTIER all the cells adjacent to C that aren’t in IN (or in
FRONTIER already)
• Continue until there are no more cells in FRONTIER
• When the maze is complete (or at any time), choose the start and finish cells
Building a maze II
Applications of MST
• Any time you want to visit all vertices in a graph at
minimum cost.
• E.g., wire routing on printed circuit boards,
• sewer pipe layout,
• road planning.
• Internet content distribution
• Provides a heuristic for traveling salesman problems.
Real Life Application of a MST
• A cable TV company is laying cable in a new neighborhood.
• If it is constrained to bury the cable only along certain paths, then
there would be a graph representing which points are connected
by those paths.
• Some of those paths might be more expensive, because they are
longer, or require the cable to be buried deeper; these paths would
be represented by edges with larger weights.
• A minimum spanning tree would be the network with the lowest total
cost.
Greedy Approach

• Like Dijkstra’s algorithm, both Prim’s and Kruskal’s algorithms


are greedy algorithms.
• The greedy approach works for the MST problem; however, it
does not work for many other problems.
U have been quiet a good
audience, I wish the very best.
32

You might also like