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

Lecture 6 Graph Theory 2

Graph theory has diverse applications in everyday life, including cybersecurity, urban planning, neuroscience, and drug design. It involves concepts such as graphs, vertices, edges, and various types of graphs like simple graphs, multigraphs, and bipartite graphs. Important principles such as the Handshaking Theorem and graph coloring techniques are utilized for scheduling and optimization problems.

Uploaded by

Dugasa Bekele
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 6 Graph Theory 2

Graph theory has diverse applications in everyday life, including cybersecurity, urban planning, neuroscience, and drug design. It involves concepts such as graphs, vertices, edges, and various types of graphs like simple graphs, multigraphs, and bipartite graphs. Important principles such as the Handshaking Theorem and graph coloring techniques are utilized for scheduling and optimization problems.

Uploaded by

Dugasa Bekele
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 134

LECTURE 6:

GRAPH THEORY
GRAPH THEORY IN EVERYDAY LIFE
Graph theory is used in cybersecurity to identify hacked or criminal servers and
generally for network security.

Graph theory, and in particular rooted tree diagrams of a genome, is used


in the evolution of SARS-CoV-2.

Determining how best to add streets to congested areas of cities uses graph
theory.

Graph theory is used in neuroscience to study brain network organization

Graph theory is used in DNA sequencing.

Design of radar and sonar systems uses graph theory via Golomb rulers.
Graph Theory in Drug Design
VARYING APPLIC ATIONS (EXAMPLES)

• Computer networks
• Distinguish between two chemical compounds with the same molecular
formula but different structures
• Pages are linked by hyperlinks on the internet
• Components of electric circuit
• Solve shortest path problems between cities
• Scheduling exams and assign channels to television stations
GRAPHS

• A graph 𝐺 = (𝑉, 𝐸) consists of 𝑉, a non-empty set of vertices and 𝐸 a set of


edges. Edges connect either one or two vertices, called endpoints.

A B Simple graph:
• each edge connects two different
not a simple graph vertices
• no two edges connect the same two
D C vertices

A simple railway tracks connecting different cities

Set of vertices and edges Vertices cannot be empty set, but edges
SOME TERMINOLOGY

Multigraph Loop Pseudograph

multiple edges an edge that may include loops


connecting the connects a vertex as well as multiple
same two vertices to itself edges
DIRECTED GRAPH - DIGRAPH

• A graph 𝐺 = (𝑉, 𝐸) where each edge is associated with pair of vertices. The
directed edge associated with (𝑎, 𝑏) begins at 𝑎 and ends at 𝑏.
SUMMARY

Type Directed edges Multiple edges Loops


Simple Graph Undirected No No
Multigraph Undirected Yes No
Pseudograph Undirected Yes Yes
Simple Directed Graph Directed No No
Directed Multigraph Directed Yes Yes
Mixed Graph Both Yes Yes
MORE TERMINOLOGY
FOR UNDIRECTED GRAPHS

deg(a)=4
• Adjacent vertices (neighbors) - (a, b) but deg(b)=2
(b, c)
b
• Incident – edge B is incident with vertices a
e and b
B
• Neighborhood: 𝑁 𝐴 = 𝑉𝑣∈𝐴 𝑁(𝑣)
N(e)={a, b, c} c
• Degree: sum of ins and outs e deg(c)=3
• Isolated (d) deg(e)=3
• Pendant (connected only once to another
vertex) (e)
a e
b d
deg(d)=0
d c
THE HAND-SHAKING THEOREM

Suppose there are 6 people in a room, and each must shake hands with every other person. How many handshakes?

Why not 30? a b


𝑎=5
𝑏=4 𝐺 𝑉, 𝐸 𝑤𝑖𝑡ℎ 𝑚 𝑒𝑑𝑔𝑒𝑠
𝑐 =3 = 15 total handshakes
𝑑=2 f
2𝑚 = ෍ deg(𝑣) c
𝑒=1
𝑣∈𝑣

2𝑚 = 30
𝑚 = 15 e d

How many edges are there if you have 10 vertices, each of degree 6? 2𝑚 = 60, 𝑚 = 30
IMPORTANT

• The following conclusions may be drawn from the Handshaking Theorem.

In any graph,

✓ The sum of degree of all the vertices is always even.


✓ The sum of degree of all the vertices with odd degree is always even.
✓ The number of vertices with odd degree are always even.
FOR DIRECTED GRAPHS

(𝑎, 𝑏)
• Adjacent to a is adjacent to b
b
• Adjacent from b is adjacent from a a
• Initial vertex a B

• Terminal / end vertex b


c
• In-degree of vertex 𝑣 = 𝑑𝑒𝑔− (𝑣) e
b: 𝑣 = 𝑑𝑒𝑔− 𝑏 = 2
• Out-degree of vertex 𝑣 = 𝑑𝑒𝑔+ (𝑣)
b: 𝑣 = 𝑑𝑒𝑔+ 𝑏 = 1
• σ𝑣∈𝑣 𝑑𝑒𝑔− 𝑣 = σ𝑣∈𝑣 𝑑𝑒𝑔+ 𝑣 = |𝐸| d
+1
+1 +1 Number of edges
REPRESENTING GRAPHS WITH MATRICES

• Reference:
https://www.simplilearn.com/tutorials/data-structure-tutorial/graphs-in-data-
structure
Adjacency matrix (vertex matrix)

DIRECTED GRAPH
Adjacency matrix (vertex matrix)
Adjacency matrix (vertex matrix)
Adjacency matrix (vertex matrix)

UNDIRECTED GRAPH
Adjacency matrix (vertex matrix)

Weighted Graph
The Vertex-Edge Incidence Matrix

𝑒1 𝑒2 𝑒3 𝑒4
𝑉1 1 1 1 0
𝑉2 1 0 0 0
𝑉3 0 1 0 1
𝑉4 0 0 1 1
a) Create a matrix considering arrows.
b) Create a matrix without arrows.
Branches
1 2 Nodes 𝟏 𝟐 𝟑 𝟒 𝟓
1 1 0 0 0 1
4 2 −1 1 0 1 0
5 3 3 0 −1 1 0 0
4 0 0 −1 −1 −1
𝒆𝟏 𝒆𝟐 𝒆𝟑 𝒆𝟒 𝒆𝟓 𝒆𝟔 𝒆𝟕
𝑉1 1 0 0 0 0 1 −1
𝑉2 −1 1 0 0 0 0 0
𝑉3 0 −1 1 0 1 0 0
𝑉4 0 0 −1 −1 0 −1 0
𝑉5 0 0 0 1 −1 0 1
Incidence Matrix

𝑒1 𝑒2 𝑒3 𝑒4

1 2 1 5 0
2 2 0 0 0
3 0 1 0 6 =
4 0 0 5 6

Weighted Graph
Adjacency Matrix

𝑽𝟏

𝑽𝟐

𝑽𝟑

Weighted Graph
HOMEWORK
HOMEWORK
SUMMARY
SUMMARY

Adjacency Matrix: rows and columns represent vertices.

undirected graph directed graph


SUMMARY

Incidence Matrix: rows represent vertices and columns represent edges.

undirected graph directed graph


COMPARE
READING

https://sitn.hms.harvard.edu/flash/2021/graph-theory-101/

https://towardsdatascience.com/what-is-graph-theory-and-why-
should-you-care-28d6a715a5c2

https://www.xomnia.com/post/graph-theory-and-its-uses-with-
examples-of-real-life-problems/
SPECIAL TYPES OF GRAPHS

Importance of different graphs in our life:

• Visual presentation of data makes it easier to understand large


amounts of data, trends, and relationships.
• The use of graphs in daily life also helps in making an analysis. For example, it
provides structure in assessing performances, sales, and even deadlines.
• Also, it helps making calculations easier.
𝐾1

COMPLETE GRAPHS

• A complete graph, denoted 𝐾𝑛 , is a simple


graph that contains exactly one edge
between each pair of 𝑛 distinct vertices.
CYCLES AND WHEELS

• A wheel 𝑊𝑛 is obtained when we add an


• A cycle 𝐶𝑛 , 𝑛 ≥ 3, consists of vertices
additional vertex to 𝐶𝑛 , and connect that
𝑣1, 𝑣2, . . , 𝑣𝑛 and edges
vertex to each existing vertex.
𝑣1 , 𝑣2 , 𝑣2, 𝑣3 , . . , 𝑣𝑛−1, 𝑣𝑛 , {𝑣𝑛 , 𝑣1}.
N-CUBES OR 𝑄3 = 23 = 8
HYPERCUBES
𝑄2 = 22 = 4

𝑄1 = 21 = 2
• An n-dimensional hypercube,
or n-cube, denoted 𝑄𝑛 , is a
graph with vertices
𝑛
representing the 2 bit strings
of length 𝑛. Adjacent vertices
differ by exactly one bit-
position.
𝑄0 = 20 = 1

Vertices differ in exactly 1 bit. They get joined by edge.


HYPERCUBES APPLICATIONS

Hypercube Network Mutations in Biology

Science
BIPARTITE
GRAPHS

• A simple graph is a called


bipartite if its vertex set, 𝑉,
can be partitioned into two
disjoint subsets such that
each edge connects a vertex
from one subset to a vertex
of the other.
BIPARTITE G RAPH APPL IC ATIONS
GRAPH COLORING

• Determine if graphs 𝐺 or 𝐻 are bipartite. Try the graph coloring technique!


Isomorphism

g
a
⟺ d
b
f
e
c

Yes, it is a bipartite!
No need to continue, it is not a bipartite graph!
COMPLETE BIPARTITE GRAPHS

• A complete bipartite graph 𝐾𝑚,𝑛 is a bipartite graph with subsets of m and n


vertices, respectively with an edge between each pair of vertices from opposite
subsets.
Suppose Adam, Ben, Chris, David and Eric are training for tasks at work. Adam
and Chris are training for task 1, Ben, Chris and Eric are training for Task 2, David
is training for task 3, Chris and Eric are training for task 4 and Eric is training for
task 5. Create a graph to model this, then determine if a matching is possible.

graph matching

A 1 𝐷−3
B 2 𝐸 −5
C 3 𝐶−4
D 4 𝐴−1
E 5 𝐵−2
has no matching has no matching

has a matching has a matching

Matching
APPLIC ATIONS OF GRAPH
COLORING

• Scheduling problems in management science


• Allocating transmission frequencies to TV and radio stations
• Study of Cell Phone traffic
• Coloring maps so that no two regions that share a boundary
are the same color
GRAPH COLORING

A coloring for a graph is a coloring of the vertices in such a way that the vertices joined by an edge
have different colors.

The chromatic number of a graph is the least number of colors needed to make a coloring.
COLORING A GRAPH

Step 1: Choose a vertex with highest degree, and color it. Use the same color to color as
many vertices as you can without coloring vertices joined by an edge of the same
color.

Step 2: Choose a new color and repeat what you did in Step 1 for vertices not already colored.

Step 3: Repeat Step 1 until all vertices are colored.


Color the graph and give its chromatic number.

Count degrees Color the highest degree Color vertices

Its chromatic number is 3


SCHEDULING

• Suppose we have several classes to offer but a limited number of class times.
Some classes cannot be offered at the same time because the same instructor
teaches the classes. What is the minimum number of classrooms needed for
the following?

Course Conflicts with


Algebra Geom, Trig
Geometry Calc, Alg
Calculus Geom, Tri, Discret
Discrete Calc, Trig
Trigonometry Discrete, Calc, Alg
Chromatic number is 3. Which means we
need 3 class times to hold these classes.
THREE GOALS OF SCHEDULING
PROBLEMS

• Optimization issues – Try to maximize profit, minimize cost.


Example: Scheduling machine time for earliest completion time

• Equity – Try to make things fair for all participants.


Example: Schedule baseball games (same number home and away games)

• Conflict Resolution – Try to prevent conflicts from happening.


Example: Scheduling college final examinations for end of term
F M H P E I S C
SCHEDULING
French (F) X X X X X
Math (M) X X X
History (H) X X X
• An education center is offering eight
courses during its summer session. The Philosophy (P) X X
table shows with an X which pairs of
courses have one or more students in
common. Only two air-conditioned English (E) X X X
lecture halls are available for use at any
one time. Italian (I) X X X X X
• To design an efficient way to schedule Spanish (S) X X
the final examinations, represent the
information in this table by using a Chemistry (C) X X X
graph. In the graph, represent courses by
vertices and join two courses by an
edge if there is any student enrolled in
both courses.
F M H P E I S C
French (F) X X X X X
Math (M) X X X
History (H) X X X
Philosophy (P) X X
English (E) X X X
Italian (I) X X X X X
Spanish (S) X X
Chemistry (C) X X X

What if only two rooms have air-conditioning?


We see that 𝑥 = 4.
This means that four times are Time Slot 1: F, H
needed. Time Slot 2: P, I
Time Slot 1: F, H Time Slot 3: C, M
Time Slot 2: P, I Time Slot 4: E, S
Time Slot 3: C, M, S
Time Slot 4: E
Corncob College elects 10 students to serve as officers on 8 committees. The list of
the members of each of the committees is:

• Corn Feed Committee: Darcie, Barb, Kyler


• Dorm Policy Committee: Barb, Jack, Anya, Kaz
• Extracurricular Committee: Darcie, Jack, Miranda
• Family Weekend Committee: Kyler, Miranda, Jenna, Natalie
• Homecoming Committee: Barb, Jenna, Natalie, Skye
• Off Campus Committee: Kyler, Jenna, Skye
• Parking Committee: Jack, Anya, Miranda
• Student Fees Committee: Kaz, Natalie

They need to schedule meetings for each of these committees, but two committees cannot
meet at the same time if they have any members in common.

a) Draw a graph representing this situation. (Hint: let the vertices represent the committees.)
b) How many different meeting times will we need?
• The mathematics department has six committees, each
meeting once a month. How many different meeting times must
be used to ensure that no member is scheduled to attend two
meetings at the same time if the committees are
• C1 = {Arlinghaus, Brand, Zaslavsky},
• C2 = {Brand, Lee, Rosen},
• C3 ={Arlinghaus, Rosen, Zaslavsky},
• C4 = {Lee, Rosen, Zaslavsky},
• C5 = {Arlinghaus, Brand},
• C6 = {Brand, Rosen, Zaslavsky}?

5 meeting times are needed


GRAPHING ONLINE TOOL

https://graphonline.ru/en/#

https://mathigon.org/course/graph-theory/introduction
HOMEWORK
HOMEWORK

The mathematics department at Cornucopia College will offer seven courses next semester:
Math 105 (M), Numerical Analysis (N), Linear Operators (O), Probability (P), Differential Equations (Q),
Real Analysis (R), Statistics (S). The department has twelve students, who will take the following classes:

Alice: N, O, Q Chaz : R, M
Bob: N, R, S Dan: N,O
Greg: M,P Emma: O, M
Homer: R,O Kate: P,S
Inez: N, Q Lara: P,Q
Fonz: N, R Jill: N,S,Q

The department needs to schedule class times for each of these courses, but two courses cannot meet at the
same time if they have any students in common. How many different class times will they need?
APPLICATIONS OF GRAPHS
Suppose 8 devices (computers, printers, etc.) must be connected through a local area network.
Let’s explore how this might look.

Star Ring Hybrid


Suppose you have 9 processors to carry out an algorithm. Describe three different ways to
arrange the processors and the advantage or disadvantage of each.

Linear Array Complete Mesh Network


TREES
• A tree is a simple, connected, undirected graph with no simple circuits. This
means there is a unique simple path between any two of its vertices.
ROOTED TREES

• A tree in which one vertex has been designated as the root and every edge is
directed away from the root. We typically place the root at the top of the tree.

Create a rooted tree from 5.


MORE TERMINOLOGY FOR ROOTED
TREES

• m-ary tree
• binary tree:
• parent: a
• child: b, c
• sibling: b and c
• ancestor: a
• descendants: b - p
• leaf: no children
• internal vertices: has children
BINARY TREE
M-ARY TREE
B ALANCED M-ARY TREES

➢ The height of a rooted tree is the maximum of the levels of vertices.

➢ A rooted m-ary tree of height ℎ is balanced if all leaves are at levels ℎ or ℎ − 1.

Example: Find the level of each vertex in the following rooted tree. What is the height of this tree?

Level: 0

Level: 1

Level: 2 ℎ=4
Not balanced: leaves
Level: 3
at ℎ − 2 also exist
Level: 4
B ALANCED M-ARY TREES

Balanced Not Balanced Balanced

Balancing the tree makes for better search times 𝑂(log(𝑛)) as opposed to 𝑂(𝑛).

An unbalanced tree is, in its worst case, just a linked list and the worst case to find an
element becomes 𝑂(𝑛), instead of 𝑂(𝑙𝑜𝑔𝑛) .

A balanced tree avoids this worst case, and ones that are nearly as bad.
PROPERTIES OF TREES

• A tree with 𝑛 vertices has 𝑛 − 1 edges


• A full 𝑚 − 𝑎𝑟𝑦 tree with 𝑖 internal vertices contains 𝑛 = 𝑚𝑖 + 1 vertices
• A full 𝑚 − 𝑎𝑟𝑦 tree with:
𝑛−1 𝑚−1 𝑛+1
i. 𝑛 vertices has 𝑖 = internal vertices and 𝑙 = leaves
𝑚 𝑚

ii. 𝑖 internal vertices has 𝑛 = 𝑚𝑖 + 1 vertices and 𝑙 = 𝑚 − 1 𝑖 + 1 leaves


(𝑚𝑙−1) 𝑙−1
iii. 𝑙 leaves has 𝑛 = 𝑚−1
vertices and 𝑖 = 𝑚−1 internal vertices
Suppose someone starts a chain letter. Each person is asked to send the letter to four other people.
Everyone does it. How many people have seen the letter, including the original sender, if no one
receives more than one letter, and the chain ends after 100 people read it but did not send it on. How
many people sent out the letter?

It is a tree, because no one receives more than one letter.


How many people sent out the letter? 𝒊 =?
leaves =100 internal vertices
𝑚𝑙 − 1
𝑚 = 4 (full m-ary tree) 𝑛=
𝑚−1 𝑙−1
𝑖=
𝑚−1
How many people have seen the letter: 𝑛 =? 4 100 − 1 399
𝑛= = = 133 100 − 1 99
4−1 3 𝑖= = = 33
4−1 3
A chain letter starts when a person sends a letter to five others. Each person who receives the
letter either sends it to five other people who have never received it or does not send it to
anyone. Suppose that 10,000 people send out the letter before the chain ends and that no one
receives more than one letter. How many people receive the letter, and how many do not
send it out?

𝑖 internal vertices has 𝑛 = 𝑚𝑖 + 1 vertices and 𝑙 = 𝑚 − 1 𝑖 + 1 leaves

𝑚 = 5 (full 5-ary tree) How many do not send it out: 𝑙 =?


𝑖 = 10000 𝑙 = 5 − 1 ∙ 10000 + 1 = 40001
How many people receive the letter: 𝑛 =?

𝑛 = 5 ∙ 10000 + 1 = 50001
❖ How many matches are played in a tennis tournament of 27 players?
26 𝑚𝑎𝑡𝑐ℎ𝑒𝑠

❖ There are 256 players in a chess tournament (singles). Two players play a match. Matches are
played on a knockout basis, the loser is eliminated after each match. How many matches
need to be played to declare a winner? There is no draw.

255 𝑚𝑎𝑡𝑐ℎ𝑒𝑠

In both questions, idea is the same:


𝑛 = 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑝𝑙𝑎𝑦𝑒𝑟𝑠 = 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑣𝑒𝑟𝑡𝑖𝑐𝑒𝑠

Find 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑒𝑑𝑔𝑒𝑠?

A tree with 𝑛 vertices has 𝑛 − 1 edges


WH I C H OF
TH ESE
GR APH S AR E
TR EES?
Answer these questions about the rooted tree illustrated.

a) Which vertex is the root?


b) Which vertices are internal?
c) Which vertices are leaves?
d) Which vertices are children of j?
e) Which vertex is the parent of h?
f ) Which vertices are siblings of o?
g) Which vertices are ancestors of m?
h) Which vertices are descendants of b?

Subtree
DIJKSTRA’S SHORTEST PATH
ALGORITHM

• Objective: to find the shortest path between any two


vertices in a graph.
USAGE OF DJIKSTRA’S
ALGORITHM
• Digital Mapping Services in Google
Maps
• Social Networking Applications
• Telephone Network
• IP routing to find Open shortest
Path First
• Flighting Agenda
• Designate file server
• Robotic Path
Find the shortest path from vertex A to every other vertex

Vertex Shortest Previous


distance vertex
from A
A 0
B 3 D
C 7 E
D 1 A
E 2 D
Consider the start vertex, A

Distance from A to A = 0 Distances to all other vertices from A are unknown, therefore ∞ (infinity)

Vertex Shortest Previous


distance vertex
from A
A 0
B ∞
C ∞
D ∞
E ∞

Visited [ ] Unvisited [A, B, C, D, E ]


Visit the unvisited vertex with the smallest known distance from the start vertex

This time around, it is vertex D

Vertex Shortest Previous


distance vertex
from A
A 0
B 6 A
C ∞
D 1 A
E ∞

Visited [A] Unvisited [B, C, D, E ]


For the current vertex, examine its unvisited neighbors

We are currently visiting D and its unvisited neighbors are B and E

For the current vertex, calculate the distance of each neighbor from the start vertex

Vertex Shortest Previous


distance vertex
from A
A 0
B 6≫3 A≫𝐷
C ∞
D 1 A
E 2 D

Visited [A, D] Unvisited [B, C, E ]


Visit the unvisited vertex with the smallest known distance from the start vertex

This time around, it is vertex E

2+2 = 4 We do not need to update the distance to B

Vertex Shortest Previous


distance vertex
2+5 = 7 from A
A 0
B 3 𝐷
C 7 E
D 1 A
E 2 D

Visited [A, D, E] Unvisited [B, C]


For the current vertex, examine its unvisited neighbors

We do not need to update the distance to C


3+5 = 8
Vertex Shortest Previous
distance vertex
from A
A 0
B 3 𝐷
C 7 E
D 1 A
E 2 D

Visited [A, D, E, B] Unvisited [C]


We are currently visiting C and it has no unvisited neighbors

Vertex Shortest Previous


distance vertex
from A
A 0
B 3 𝐷
C 7 E
D 1 A
E 2 D

Visited [A, D, E, B, C] Unvisited [ ]


ALGORITHM

• Let distance of start vertex from start vertex = 0


• Let distance of all other vertices from start = ∞ (infinity)

Repeat
• Visit the unvisited vertex with the smallest known distance from the start vertex
• For the current vertex, examine its unvisited neighbors
• For the current vertex, calculate distance of each neighbor from start vertex
• If the calculated distance of a vertex is less than the known distance, update the
shortest distance
• Update the previous vertex for each of the updated distances
• Add the current vertex to the list of visited vertices
Until all vertices visited
ALGORITHM

• Let distance of start vertex from start vertex = 0


• Let distance of all other vertices from start = ∞ (infinity)

WHILE vertices remain unvisited


• Visit unvisited vertex with the smallest known distance from the start vertex (call this current vertex)
• FOR each unvisited neighbor of the current vertex
• Calculate distance from start vertex
• If the calculated distance of this vertex is less than the known distance
• Update the shortest distance to this vertex
• Update the previous vertex with the current vertex
• end if
• NEXT unvisited neighbor
• Add the current vertex to the list of visited vertices
END WHILE
PRACTICE

Find the shortest path from vertex A to all


other vertices.
TRY IT

Find the shortest path from vertex A to all


Show your solution in a table other vertices.
format as shown below.

Vertex Shortest Previous


distance vertex
from A
a 0
b
c
d
e
z
CONTINUE THE SOLUTION
TRY IT

Find the shortest path from vertex A to all other vertices.

Show your solution in a table format


as shown below (for each vertex).

a b c d e f z
a
Find the shortest path from 𝑉1 to 𝑉6 .

Find the shortest path from


vertex 1 to any other vertex.

Show your solution in a table


format (for each vertex).
Find the shortest path from vertex a to each vertex using Dijkstra’s algorithm.
Dijkstra’s algorithm
Finding the shortest path from a vertex to any other vertices.

Show your solution in a


table format (both).
Use Dijkstra’s algorithm to find the shortest route
from A to J. State your shortest route and its length.

Route: A C F E G J
Length: 53 km
HOME
WO RK

Use Dijkstra’s algorithm to find the shortest route from A to any other
vertices. State your shortest route and its length.
Kütahya is a city famous for its ceramics. A company located in Kütahya will distribute the
HOME ceramic dinner sets it produced to other provinces in the Aegean Region. The
distribution truck of each province is separate. A route for each truck is required. What
WORK are the shortest distance routes to the seven provinces in the region starting from
Kütahya? Show your answer by creating a model. To solve the problem, you may use the
map below and the distances between the provinces.
Recall: trees are graphs that have no circuits.
Subgraph

A subgraph for a graph is a graph whose vertices and edges are


subsets of the original graph.

Not a tree

Tree
SPANNING TREES

• A spanning tree for a graph is a subgraph that includes every vertex of the
original and is a simple tree with no cycles and loops.
Find the spanning tree for the graph below.

This graph has 8 vertices, 9 edges and 2 circuits.

A spanning tree for 8 vertices must have 7 edges.

We must remove 2 edges and break 2 circuits.

We break the two circuits by removing a single edge from each. Two possibilities of many:
Find all spanning tree for the graph below.

This graph has 7 vertices, 7 edges and 1 circuit.

A spanning tree for 7 vertices must have 6 edges.

We must remove 1 edge and break the circuit.


Find 3 different spanning trees for the graph below.

This graph has 5 vertices, 7 edges, multiple overlapping circuits.

A spanning tree for 5 vertices must have 4 edges.

We must remove 3 edges and break the circuits.

All of these are subgraphs of


the original graph, have no
circuits, and include all the
vertices. So, they are
spanning trees.
A weighted graph is called a network.

MINIMUM SPANNING TREE ( MST)

• total / sum of weights of edges must be minimum.


Recall: trees cannot form a cycle Define MST
Define MST

A spanning tree for 5 vertices must have 4 edges.


TRY IT
IF T HE R E AR E T HO U SAN D S
O F V E RT IC ES AN D E D GE S,
HOW WILL IT BE SO LV E D ?

Algorithms:
Prim’s Algorithm
Kruskal’s Algorithm
PRIM'S ALGORITHM

To find the MST we can use Prim’s Algorithm (Greedy Algorithm)

• Basically, Prim’s algorithm is a modified version of Dijkstra’s algorithm. First, we


choose a node to start from and add all its neighbors to a priority queue.

• Step 1:
Select any node to be the first of T.
• Step 2:
Consider which arcs connects nodes in T to nodes outside T. Pick 1 with
minimum weight (if more than 1, choose any). Add this arc and node to T.
• Step 3:
Repeat Step 2 until T contains every node of the graph.
Weight:

7 + 2 + 4 + 5 = 18
Converting a network to matrix form

• Step 1:
Select any node to be the first of T.
• Step 2:
Circle the new node of T in the top row and cross out the row corresponding to this
new node.
• Step 3:
Find the smallest weight left in the columns of the nodes of T. Circle this weight.Then
choose the node whose row the weight is in to join T. If several choose any.
• Step 4
Repeat steps 2 and 3 until T contains every node.

𝐴 𝐵 𝐶 𝐷 𝐸
𝐴 − 6 4 8 2
𝐵 6 − 5 8 6 Weight:
𝐶 4 5 − 9 4 7 + 2 + 4 + 5 = 18
𝐷 8 8 9 − 7
𝐸 2 6 4 7 −
Step 1 - Remove all loops and parallel edges

Step 2 - Choose any arbitrary node as root node

Step 3 - Check outgoing edges and select the one


with less cost
Prim’s Algorithm
Consider the following pseudocode for Prim’s algorithm.
KRUSKAL'S ALGORITHM

To find the MST of a graph with n nodes

• Step 1:
Choose the arc of least weight.
• Step 2:
Choose from those arcs remaining the arc of least weight which do not form a
cycle. (if more than 1, choose any)
• Step 3:
Repeat Step 2 until (n-1) arcs have been chosen.
𝐴𝐸 = 2
𝐸𝐶 = 4
= 18
𝐸𝐷 = 7
𝐵𝐶 = 5
Find the Minimum Spanning Tree using Kruskal’s Algorithm:
Answer

Find the Minimum Spanning Tree using Kruskal’s Algorithm:


Take a look at the pseudocode for Kruskal’s algorithm.
What is difference between Prims and Kruskal algorithm?

➢ Prim's Algorithm grows a solution from a


random vertex by adding the next cheapest
vertex to the existing tree.

➢ Kruskal's Algorithm grows a solution from the


cheapest edge by adding the next cheapest edge
to the existing tree / forest.
Let’s highlight some key differences between the two algorithms.

As we can see, the Kruskal algorithm is better to use regarding the easier
implementation and the best control over the resulting MST. However,
Prim’s algorithm offers better complexity.
PRIM’S & KRUSKAL’S ALGORITHMS IN
REAL LIFE
APPLIC ATIONS

APPLICATIONS WHERE KRUSKAL’S APPLICATIONS WHERE PRIM’S


ALGORITHM IS GENERALLY USED: ALGORITHM IS GENERALLY USED:
• 1. Landing cables • 1. All the applications stated in the Kruskal’s
algorithm’s applications can be resolved using Prim’s
• 2.TV Network algorithm (use in case of a dense graph).
• 3.Tour Operations • 2. Network for roads and Rail tracks connecting all
• 4. LAN Networks the cities.

• 5. A network of pipes for drinking water or natural • 3. Irrigation channels and placing microwave towers
gas. • 4. Designing a fiber-optic grid or ICs.
• 6. An electric grid • 5.Travelling Salesman Problem.
• 7. Single-link Cluster • 6. Cluster analysis.
• 7. Pathfinding algorithms used in AI(Artificial
Intelligence).
• 8. Game Development
• 9. Cognitive Science
Time for Practice
Find MST using:
a) Prim’s Algorithm
b) Kruskal’s Algorithm
Homework

You might also like