Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
34 views60 pages

Lec 15

The document discusses various applications of graphs, including transportation, network design, robotics, urban planning, and biology. It explains concepts like weighted graphs, shortest-path algorithms (specifically Dijkstra's algorithm), the Traveling Salesperson Problem, and spanning trees, including minimum spanning trees and algorithms for generating them such as Prim's and Kruskal's. The document emphasizes the importance of these graph-related concepts in optimizing routes, communication networks, and other practical scenarios.

Uploaded by

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

Lec 15

The document discusses various applications of graphs, including transportation, network design, robotics, urban planning, and biology. It explains concepts like weighted graphs, shortest-path algorithms (specifically Dijkstra's algorithm), the Traveling Salesperson Problem, and spanning trees, including minimum spanning trees and algorithms for generating them such as Prim's and Kruskal's. The document emphasizes the importance of these graph-related concepts in optimizing routes, communication networks, and other practical scenarios.

Uploaded by

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

LECTURE NO 15

Graphs
Chapter 10
1. Transportation and Navigation
Route Planning: Determining the fastest or shortest route between
locations in GPS navigation systems (e.g., Google Maps, Waze).
Public Transport Systems: Optimizing bus or train routes for
minimum travel time or distance.
Logistics and Delivery: Designing efficient routes for package
delivery, reducing costs and delivery time.
2. Network Design and Communication
Routing in Computer Networks: Finding the shortest data
transmission path in networks using algorithms like Dijkstra or
Bellman-Ford Internet Protocols: Used in OSPF (Open Shortest
Path First) and RIP (Routing Information Protocol) to manage
data packet routing.
Telecommunication Networks: Minimizing latency in call routing
or signal transmission.
3. Robotics and AI
Pathfinding in Robotics: Guiding robots in warehouses, factories,
or autonomous vehicles through the most efficient routes.
Game Development: AI pathfinding for characters and objects in
video games.
4. Urban Planning and Infrastructure
Traffic Management: Optimizing flow to reduce congestion and
travel times.
Utility Networks: Designing efficient layouts for pipelines,
electricity grids, and water supply systems.
5. Biology and Medicine
Protein Interaction Networks: Analyzing biochemical pathways to
determine the most efficient interactions.
Gene Sequencing: Finding efficient alignments or matches in
DNA and protein sequences.
weighted graphs
Graphs that have a number assigned to each edge are called
weighted graphs. Weighted graphs are used
• To model computer networks. Communications costs (such as
the monthly cost of leasing a telephone line), the response times
of the computers over these lines, or the distance between
computers, can all be studied using weighted graphs.
• Several types of problems involving weighted graphs arise
frequently. Determining a path of least length between two
vertices in a network is one such problem.
Length of a path in a weighted graph be the sum of the weights of
the edges of this path. (The reader should note that this use of the
term length is different from the use of length to denote the number
of edges in a path in a graph without weights.)

The question is: What is a shortest path, that is, a path of least
length, between two given vertices?
Can we answer the following questions by looking at figure
no.1

• what is a shortest path in air distance between Boston and


Los Angeles?
• What combinations of flights has the smallest total flight
time (that is, total time in the air, not including time between
flights) between Boston and Los Angeles?
• What is the cheapest fare between these two cities?

Another important problem involving weighted graphs asks for


a circuit of shortest total length that visits every vertex of a
complete graph exactly once.

This is the famous traveling salesperson problem, which asks


for an order in which a salesperson should visit each of the
cities on his route exactly once so that he travels the minimum
total distance.
A Shortest-Path Algorithm
There are several different algorithms that find a shortest path
between two vertices in a weighted graph. We will present a
greedy algorithm discovered by the Dutch mathematician Edsger
Dijkstra in 1959. The version we will describe solves this
problem in undirected weighted graphs where all the weights are
positive. It is easy to adapt it to solve shortest-path problems in
directed graphs.
Note that a shortest path between vertices could have been
found by a brute force approach by examining the length of
every path between any two vertices. However, this brute force
approach is impractical for humans and even for computers for
graphs with a large number of edges.

Dijkstra’s algorithm proceeds by finding the

• length of a shortest path from a to a first vertex,


• the length of a shortest path from a to a second vertex,
• and so on,
• until the length of a shortest path from a to every vertex is
found.
Dijkstra's Algorithm: A Simple Explanation
Dijkstra’s algorithm is a way to find the shortest path from one
starting node to all other nodes in a graph. The graph consists of
nodes (points) connected by edges (lines), and each edge has a
weight (cost, distance, or time). The goal is to minimize the total
cost from the starting node to other nodes.
How It Works (Step-by-Step)
1.Start with the Source Node: Begin at the starting node, setting
its distance to 0 (since the distance to itself is zero). All other nodes
start with a distance of infinity (∞), meaning they're not yet
reachable.
2.Visit the Nearest Node: From the current node, look at all its
neighbors and calculate the cost to reach each neighbor through the
current node.
3.Update Shortest Distances: If the calculated cost to reach a
neighbor is less than its current known cost, update the cost.
4.Mark the Node as Visited: Once all neighbors of the current
node are checked, mark the node as "visited." Visited nodes won’t
Key Points

•Dijkstra's algorithm works only with graphs


that have non-negative weights.

•It finds the shortest path from the starting


node to all other nodes (not just one).

EXAMPLE 2
Use Dijkstra’s algorithm to find the length of a shortest path
between the vertices a and z in the weighted graph displayed
in Figure 4(a).
Vertex Shortest Length Previous Vertex
Shortest
distance Finalized
from a previous
vertex
a 0 0 a a
b , 4, 3 3 a, c c
c ,2 2 a a
d ,10,8 8 c, b b
e ,12,10 10 c,d d
z ,14,13 13 d, e e
THEOREM 1 Dijkstra’s algorithm finds the length of a shortest
path between two vertices in a connected simple undirected
weighted graph.
The Traveling Salesperson Problem
For example, suppose that the salesperson wants to visit Detroit,
Toledo, Saginaw, Grand Rapids, and Kalamazoo (see Figure 5). In
which order should he visit these cities to travel the minimum total
distance?
The Traveling Salesperson Problem
A traveling salesperson wants to visit each of n cities exactly once
and return to his starting point. For example, suppose that the
salesperson wants to visit Detroit, Toledo, Saginaw, Grand Rapids,
and Kalamazoo (see Figure 5). In which order should he visit these
cities to travel the minimum total distance?
To solve this problem we can assume the salesperson starts in Detroit
(because this must be part of the circuit) and examine all possible
ways for him to visit the other four cities and then return to Detroit
(starting elsewhere will produce the same circuits). There are a total
of 24 such circuits, but because we travel the same distance when we
travel a circuit in reverse order, we need only consider 12 different
circuits to find the minimum total distance he must travel. We list
these 12 different circuits and the total distance traveled for each
circuit. As can be seen from the list, the minimum total distance of
458 miles is traveled using the circuit Detroit–Toledo–Kalamazoo–
Grand Rapids–Saginaw–Detroit (or its reverse)
The most straightforward way to solve an instance of the
traveling salesperson problem is to examine all possible
Hamilton circuits and select one of minimum total length.

How many circuits do we have to examine to solve the problem


if there are n vertices in the graph?

Once a starting point is chosen, there are different Hamilton


circuits to examine, because there are n − 1 choices for the
second vertex, n − 2 choices for the third vertex, and so on.
Because a Hamilton circuit can be traveled in reverse order, we
need only examine circuits to find our answer.
Note that grows extremely rapidly.

Trying to solve a traveling salesperson problem in this way when


there are only a few dozen vertices is impractical.

For example,
• with 25 vertices, a total of (approximately 3.1 × 1023)
different Hamilton circuits would have to be considered.

• If it took just one nanosecond ( second) to examine each


Hamilton circuit, a total of approximately ten million years
would be required to find a minimum-length Hamilton circuit
in this graph by exhaustive search techniques.
Traveling Salesman Problem (TSP): An Easy Explanation
The Traveling Salesman Problem (TSP) is a famous optimization
problem.
Here’s the scenario:
A salesman wants to visit a list of cities, starting from one city, visiting
each city exactly once, and returning to the starting city. The goal is to
find the shortest possible route that visits all cities.
Steps to Understand TSP
1.Understand the Problem:
1. You have a list of cities connected by roads.
2. Each road has a "cost" (e.g., distance, time, or money).
3. The salesman must visit all cities and return to the starting city.
2.Objective:
1. Find the route with the minimum total cost.
3.Constraints:
1. Visit each city exactly once.
2. Return to the starting city after visiting all cities.
A connected graph that contains no simple circuits is called a tree.
Trees were used as long ago as 1857, to count certain types of
chemical compounds. Since that time, trees have been employed to
solve problems in a wide variety of disciplines, as the examples in
this chapter will show.
• Trees are used to construct efficient algorithms for locating items
in a list.
• They can be used in algorithms, such as Huffman coding, that
construct efficient codes saving costs in data transmission and
storage.
• Trees can be used to study games such as checkers and chess and
can help determine winning strategies for playing these games.
In
this chapter we will focus on a
particular type of graph called a tree,
so named because such
graphs resemble trees. For example,
family trees are graphs that represent
genealogical charts.
Family trees use vertices to represent
the members of a family and edges to
represent parent–
child relationships. The family tree of
the male members of the Bernoulli
family of Swiss
mathematicians is shown in Figure 1.
The undirected graph representing a
family tree (restricted
to people of just one gender and with
no inbreeding) is an example of a
tree.
DEFINITION 1 A tree is a connected undirected graph with
no simple circuits. Because a tree cannot have a simple circuit,
a tree cannot contain multiple edges or loops. Therefore any
tree must be a simple graph.

EXAMPLE 1 Which of the graphs shown in Figure 2 are trees?


Solution: and are trees, because both are connected graphs with
no simple circuits. is not a tree because e, b, a, d, e is a simple
circuit in this graph. Finally, is not a tree because it is not
connected.
Any connected graph that contains no simple circuits is a tree.
What about graphs containing no simple circuits that are not
necessarily connected? These graphs are called forests and have
the property that each of their connected components is a tree.
Figure 3 displays a forest.
THEOREM 1 An undirected graph is a tree if and only if there
is a unique simple path between any two of its vertices.
A rooted tree is a tree in which one vertex has
been designated as the root and every edge is
directed away from the root.
The terminology for trees has botanical and genealogical origins.
Suppose that T is a rooted tree. If v is a vertex in T other than the
root, the parent of v is the unique vertex u such that there is a
directed edge from u to v (the reader should show that such a
vertex is unique). When u is the parent of v, v is called a child of
u. Vertices with the same parent are called siblings. The ancestors
of a vertex other than the root are the vertices in the path from the
root to this vertex, excluding the vertex itself and including the
root (that is, its parent, its parent’s parent, and so on, until the root
is reached). The descendants of a vertex v are those vertices that
have v as an ancestor. A vertex of a rooted tree is called a leaf if it
has no children.
Vertices that have children are called internal vertices. The root is
an internal vertex unless it is the only vertex in the graph, in which
case it is a leaf. If a is a vertex in a tree, the subtree with a as its
root is the subgraph of the tree consisting of a and its descendants
and all edges incident to these descendants.
A rooted tree is called an m-ary tree if every internal vertex has no
more than m children. The tree is called a full m-ary tree if every
internal vertex has exactly m children. An m-ary tree with m = 2 is
called a binary tree.

EXAMPLE 3 Are the rooted trees in Figure 7 full m-ary trees


for some positive integer m?
Consider the system of roads in Maine represented by the simple
graph shown in Figure 1(a). The only way the roads can be kept
open in the winter is by frequently plowing them. The highway
department wants to plow the fewest roads so that there will
always be cleared roads connecting any two towns. How can this
be done? At least five roads must be plowed to ensure that there
is a path between any two towns.
Let G be a simple graph. A spanning tree of G is a subgraph of G
that is a tree containing every vertex of G.

EXAMPLE 1
Find a spanning tree of the simple graph G shown in Figure 2.
Solution:
The graph G is connected, but it is not a tree because it contains
simple circuits. Remove the edge {a, e}. This eliminates one
simple circuit, and the resulting subgraph is still connected and still
contains every vertex of G. Next remove the edge {e, f } to
eliminate a second simple circuit. Finally, remove edge {c, g} to
produce a simple graph with no simple circuits. This subgraph is a
spanning tree, because it is a tree that contains every vertex of G.
The sequence of edge removals used to produce the spanning tree
is illustrated in Figure 3.
The tree shown in Figure 3 is not the only spanning tree of G.
For instance, each of the trees shown in Figure 4 is a spanning
tree of G.
THEOREM 1 A simple graph is connected if and only if it has a
spanning tree.

A company plans to build a communications network connecting


its five computer centers. Any pair of these centers can be linked
with a leased telephone line.

Which links should be made to ensure that there is a path


between any two computer centers so that the total cost of the
network is minimized?
We can model this problem using the weighted graph shown in
Figure 1, where vertices represent computer centers, edges
represent possible leased lines, and the weights on edges are the
monthly lease rates of the lines represented by the edges by finding
a spanning tree so that the sum of the weights of the edges of the
tree is minimized. Such a spanning tree is called a minimum
spanning tree.
A minimum spanning tree in a connected weighted graph is
a spanning tree that has the smallest possible sum of weights
of its edges.

Algorithms to generate minimum spanning trees

1. Prims Algorithm
2. Kruskals Algorithm

Key Differences:
•Prim's Algorithm grows the Minimum Spanning Tree by
adding one edge at a time connected to the current tree.

•Kruskal's Algorithm works by globally selecting the


smallest edge without necessarily connecting to the current
tree.
Prim's Algorithm (Step-by-Step):

1.Choose a Starting Node: Begin with any node in the graph.


2.Initialize the Tree: Add the starting node to the Minimum
Spanning Tree (MST).
3.Find the Smallest Edge: Look at all edges connecting nodes in
the MST to nodes outside the MST. Pick the smallest weight edge.
4.Add the Edge and Node: Add the chosen edge and its connected
node to the MST.
5.Repeat: Continue finding and adding the smallest edge that
connects a new node to the MST.
6.Stop: When all nodes are included in the MST, the process is
complete.
Kruskal's Algorithm (Step-by-Step):

1.List All Edges: Write down all edges of the graph along with
their weights.
2.Sort Edges: Sort the edges in ascending order based on weight.
3.Initialize MST: Start with an empty set for the MST.
4.Pick the Smallest Edge: Choose the smallest weight edge from
the sorted list.
5.Check for Cycles:
1. If adding the edge forms a cycle, skip it.
2. Otherwise, include the edge in the MST.
6.Repeat: Continue adding edges, checking for cycles each time,
until the MST has n−1n-1n−1 edges (where nnn is the number of
nodes).
7.Stop: Once the MST is complete, the algorithm ends.
EXAMPLE 1
Use Prim’s algorithm to design a minimum-cost communications
network connecting all the computers represented by the graph in
Figure 1.
Solution:
We solve this problem by finding a minimum spanning tree in the
graph in Figure 1. Prim’s algorithm is carried out by choosing an
initial edge of minimum weight and successively adding edges of
minimum weight that are incident to a vertex in the tree and that
do not form simple circuits. The edges in color in Figure 2 show a
minimum spanning tree produced by Prim’s algorithm, with the
choice made at each step displayed.
EXAMPLE 2 Use Prim’s algorithm to find a minimum spanning
tree in the graph shown in Figure 3.
Solution: A minimum spanning tree constructed using Prim’s
algorithm is shown in Figure 4. The successive edges chosen are
displayed.
Is the Minimum Spanning Tree Unique?
The minimum spanning tree (MST) is unique if all the edge weights
in the graph are distinct.
•If the graph contains edges with the same weight, multiple MSTs may
exist because different algorithms or decisions during the process might
select different edges.

Will Prim's and Kruskal's Algorithms Form the Same MST?


•Yes, if the MST is unique (i.e., all edge weights are distinct), both
Prim's and Kruskal's algorithms will result in the same MST.
•No, if there are multiple valid MSTs (due to edges with equal weights),
the specific MST formed by Prim's and Kruskal's algorithms may
differ, depending on the choices made during execution. However, both
will yield a correct MST with the same total weight.
Why Might the Results Differ?

•Prim's Algorithm builds the MST incrementally by growing a


single tree, so the choice of the starting node or tie-breaking
strategies can affect the result.

•Kruskal's Algorithm processes edges globally in ascending


order of weight, and ties are resolved based on their order in the
sorted list or implementation details.
Applications of Minimum
Spanning Tree (MST)
1. Network Design
MST is used to design efficient networks by connecting
points (like cities, computers, or devices) using the least total
cost.
•Example:
• Telecommunication Networks: Connecting multiple
telephone exchanges or cell towers with the minimum
cable or fiber length.
• Computer Networks: Building a local area network
(LAN) using the least amount of wiring or connecting
servers with minimal cost.
•Why MST?: It ensures all nodes are connected with the
minimum cost, avoiding unnecessary redundancy.
2. Transportation Networks
MST helps in planning roads, railways, or pipelines to connect
various cities or stations while minimizing construction costs.
•Example:
• Designing a railway system that connects all major cities
with the least track length.
• Building a water distribution system to reach multiple areas
at minimum cost.
3. Cluster Analysis in Data Mining
MST is used to group similar data points or divide a dataset into
clusters in a way that reduces redundancy.
•Example:
• Hierarchical Clustering: Forming groups of similar items
(e.g., customers with similar shopping habits) in fields like
market analysis or bioinformatics.
•Why MST?: It simplifies the connections among data points to
focus on key relationships.
. 4. Circuit Design
MST is used in designing the layout of electrical circuits to
minimize the wiring cost and reduce redundancy.
•Example: Connecting components in a chip or PCB (Printed
Circuit Board) using the least amount of wire.
5. Image Processing
MST is used for tasks like image segmentation, where an image is
divided into meaningful regions.
•Example:
• Separating objects in a photo or satellite image by grouping
pixels with similar properties (color or brightness).
6. Traveling Salesman Problem (Approximation)
MST provides an approximation for solving the Traveling Salesman
Problem (TSP), where the goal is to visit all cities with the shortest
possible route.
•Example:
• Delivering goods to multiple cities with minimum travel
In all these applications, MST ensures efficiency
by minimizing costs, distances, or connections
while maintaining full connectivity.

It is especially useful for problems that involve


optimizing resources or infrastructure.

You might also like