Lecture 6 Graph Theory 2
Lecture 6 Graph Theory 2
GRAPH THEORY
GRAPH THEORY IN EVERYDAY LIFE
Graph theory is used in cybersecurity to identify hacked or criminal servers and
generally for network security.
Determining how best to add streets to congested areas of cities uses graph
theory.
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 B Simple graph:
• each edge connects two different
not a simple graph vertices
• no two edges connect the same two
D C vertices
Set of vertices and edges Vertices cannot be empty set, but edges
SOME TERMINOLOGY
• A graph 𝐺 = (𝑉, 𝐸) where each edge is associated with pair of vertices. The
directed edge associated with (𝑎, 𝑏) begins at 𝑎 and ends at 𝑏.
SUMMARY
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?
2𝑚 = 30
𝑚 = 15 e d
How many edges are there if you have 10 vertices, each of degree 6? 2𝑚 = 60, 𝑚 = 30
IMPORTANT
In any graph,
(𝑎, 𝑏)
• Adjacent to a is adjacent to b
b
• Adjacent from b is adjacent from a a
• Initial vertex a B
• 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
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
COMPLETE GRAPHS
𝑄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
Science
BIPARTITE
GRAPHS
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
graph matching
A 1 𝐷−3
B 2 𝐸 −5
C 3 𝐶−4
D 4 𝐴−1
E 5 𝐵−2
has no matching has no matching
Matching
APPLIC ATIONS OF 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.
• 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?
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}?
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.
• 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.
• 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
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
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
𝑛 = 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 𝑚𝑎𝑡𝑐ℎ𝑒𝑠
Subtree
DIJKSTRA’S SHORTEST PATH
ALGORITHM
Distance from A to A = 0 Distances to all other vertices from A are unknown, therefore ∞ (infinity)
For the current vertex, calculate the distance of each neighbor from the start vertex
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
a b c d e f z
a
Find the shortest path from 𝑉1 to 𝑉6 .
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
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.
We break the two circuits by removing a single edge from each. Two possibilities of many:
Find all spanning tree for the graph below.
Algorithms:
Prim’s Algorithm
Kruskal’s Algorithm
PRIM'S ALGORITHM
• 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 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
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
• 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