Graphs
Hamilton Path and Hamilton Circuit in graphs
Let’s Try!
Azlina Binti Morshidi/JMSK/PKS
Graphs
Travelling Salesman Problem (TSP)
The Travelling Salesman Problem is related to the problem of
finding a Hamiltonian cycle in graph.
The problem is given a weighted graph G, find a minimum length
Hamiltonian cycle in G.
Consider the following problem : A travelling salesperson wants to
visit each of n cities exactly once and return to his starting point.
Given an undirected graph, and a cost for each edge of the
graph, find a Hamiltonian circuit of minimum total cost.
Given an undirected graph, and a distance each pair of
cities, find a minimize total distance travelled.
Azlina Binti Morshidi/JMSK/PKS
Graphs
Travelling Salesman Problem (TSP) : Example 1
Given a complete undirected graph = ( , ) that nonnegative
integer distance associated with each edge, the problem is to find
a Hamiltonian cycle of G with the shortest distance from starting at
cities 1.
10 km
5 km
15 km
10 km 8 km
9 km 8 km
8 km
15 km
Azlina Binti Morshidi/JMSK/PKS
Graphs
Travelling Salesman Problem (TSP) : Example
Method 1 : 1 → 4 → 2 → 5 → 6 → 3 → 1
Total distance : 15km + 10km + 8km + 15km + 9km + 5km = 62km
5 km 15 km
9 km
10 km 8 km
15 km
Azlina Binti Morshidi/JMSK/PKS
Graphs
Travelling Salesman Problem (TSP) : Example
Method 2 : 1 → 2 → 5 → 4 → 6 → 3 → 1
Total distance : 10km + 8km + 8km + 8km + 9km + 5km = 48km
10 km
5 km Method 2
is the
8 km
shortest
9 km 8 km
8 km
Azlina Binti Morshidi/JMSK/PKS
Graphs
Travelling Salesman Problem (TSP) : Example 2
The weighted graphs in the following figure show some major
roads in New Jersey.
Part (a) show the distances between cities on these roads.
Find the shortest route between Town A and Town E using these
roads.
20 km
42 km
Let’s Try! 30 km 35 km
60 km 40 km
55 km 75 km
45 km
85 km
Azlina Binti Morshidi/JMSK/PKS
Graphs
Travelling Salesman Problem (TSP) : Example 2
20 km
42 km
40 km
55 km 75 km
85 km
Method 1: A->B->G->C->D->F->E
20KM+42KM+40KM+75KM+55KM+85KM=317KM
Azlina Binti Morshidi/JMSK/PKS
Graphs
Travelling Salesman Problem (TSP) : Example 2
20 km
30 km 35 km
40 km
55 km
45 km
Method 2: A->B->C->G->F->D->E
20KM+35KM+40KM+30KM+55KM+45KM=225KM
Azlina Binti Morshidi/JMSK/PKS
Graphs
Travelling Salesman Problem (TSP) : Example 2
The weighted graphs in the following figure show some major
roads in New Jersey.
Part (b) show the tolls.
Find a least expensive route in term of total tolls using the roads
between Town A and Town E.
Rm1.00 Rm0.60
Rm0.70 Rm1.25
Let’s Try! Rm0.00 Rm0.00
Rm1.25 Rm1.25
Rm0.75
Rm0.00
Azlina Binti Morshidi/JMSK/PKS
Graphs
Travelling Salesman Problem (TSP) : Example 2
Rm0.60
Rm1.00
Rm0.00
Rm1.25 Rm1.25
Rm0.00
Method 2: A->B->G->C->D->F->E
RM0.60+RM1.00+RM0+RM1.25+RM1.25+RM0 =RM4.10
Azlina Binti Morshidi/JMSK/PKS
Graphs
Travelling Salesman Problem (TSP) : Example 2
Rm0.60
Rm0.70
Rm0.00 Rm0.00
Rm1.25
Rm0.75
Method 2: A->B->G->C->D->F->E
RM0.60+RM0+RM0.70+RM0+RM1.25+RM0.75 =RM3.30
Azlina Binti Morshidi/JMSK/PKS
Trees leaves
Describe Trees
branches
root
Nature view of tree
Azlina Binti Morshidi/JMSK/PKS
Trees root
Describe Trees
nodes
branches
leaves
Computer scientist’s view
Azlina Binti Morshidi/JMSK/PKS
Trees
Describe Trees
Tree is one type of graph because it resembles a tree.
A tree is a connected undirected graph with
no simple circuits.
No multiple edges
No loops.
Therefore, any tree must be a simple graph.
An undirected graph is a tree if and only if there is a unique
simple path between any two of its vertices
Consists of nodes with a parent-child relation.
Azlina Binti Morshidi/JMSK/PKS
Trees
Describe Trees
Which of the graphs below are trees?
Azlina Binti Morshidi/JMSK/PKS
Trees
Application of trees
Azlina Binti Morshidi/JMSK/PKS
Trees
Describe Trees
Examples of tree
Azlina Binti Morshidi/JMSK/PKS
Trees
Define Trees
Examples of not tree
Azlina Binti Morshidi/JMSK/PKS
Trees
Describe Trees
Examples of not tree
Azlina Binti Morshidi/JMSK/PKS
Trees
Properties of Trees
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.
Different choice of root produce different rooted tree.
Azlina Binti Morshidi/JMSK/PKS
Trees
Tree Terminology
If v is a vertex in a rooted tree other than the root, the parent of v
is the unique vertex u such that there is a directed edge from u to
v.
When u is the parent of v, v is called the child of u.
Vertices with the same parent are called siblings.
A vertex of a tree is called a leaf if it has no children.
Vertices that have children are called internal vertices
The descendants of a vertex v are those vertices that have v as
an ancestor.
Azlina Binti Morshidi/JMSK/PKS
Trees
Tree Terminology A
B CD
Root :node without parent(A)
Siblings : nodes share the same parent EF GH
Internal node : node with at least one child (A,
I JK
B, C, F)
External node (leaf) : node without children (E,
I, J, K, G, H, D)
Ancestors of a node : parent, grandparent,
grand-grandparent, etc
Descendant of a node : child, grandchild,
grand-grandchild, etc
Depth of a node : number of ancestors (Depth
of A is 0)
Height of a tree : maximum depth of any node
(3)
Degree of a node : the number of its children
Degree of a tree : the maximum number of its
node.
Subtree : tree consisting of a node and its
descendants
Azlina Binti Morshidi/JMSK/PKS
Trees A G
BF
Properties of rooted tree
C HIJ
The parent of C is …
The children of g are …
The siblings of h are ...
The ancestors of e are …
The internal vertices are …
The leaves are …
Vertex a is at level …
Vertices D and E is at level …
The height of the tree is …
Descendent of b are …
DE K LM
Azlina Binti Morshidi/JMSK/PKS
Trees
Spanning Tree
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.
A simple graph with a spanning tree must be connected because
there is a path in the spanning tree between any two vertices.
Find a spanning tree of the simple graph G in the figure below
Azlina Binti Morshidi/JMSK/PKS
Trees
Spanning Tree
Total of vertex =7
Total of edges =6 (Formula is vertex -1)
Azlina Binti Morshidi/JMSK/PKS
Trees
Spanning Tree
Spanning tree of G.
Azlina Binti Morshidi/JMSK/PKS
Trees
Spanning Tree (In Data Network)
Spanning trees play an important role in multicasting over Internet
Protocol (IP) networks.
To send data from a source computer to multiple receiving
computers, each of which is a sub network, data could be set
separately to each computer. This type of networking, called
unicasting, is inefficient, because many copies of the same data
are transmitted over the network.
To make the transmission of data to multiple receiving computers
more efficient, IP multicasting is used.
With multicasting, a computer sends a single copy of data over
the network, and as data reaches intermediate routers, the data
are forwarded to one or more other routers so that ultimately all
receiving computers in their various sub networks receive these
data.
For data to reach receiving computers as quickly as possible,
there should be no loops(circuit or cycle) in the path.
Azlina Binti Morshidi/JMSK/PKS
Trees
Spanning Tree (In Data Network)
= routers
= source
= subnetwork
Azlina Binti Morshidi/JMSK/PKS
Trees
Spanning Tree (In Data Network)
= routers
= source
= subnetwork
Azlina Binti Morshidi/JMSK/PKS
Trees
Spanning Tree (In Data Network)
= routers
= source
= subnetwork
Azlina Binti Morshidi/JMSK/PKS
Trees
Minimal 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
hat the total cost of the network is minimized?
We can model this problem using the weighted graph, where
vertices represent computer centers, edges represent possible
leased lined, and the weighted on edges are the monthly lease
rates of the lines represented by the edges. We can solve this
problem 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 trees.
Minimal spanning in a connected weighted graph is a
spanning tree that has the smallest possible sum of weight of
its edges.
Azlina Binti Morshidi/JMSK/PKS
Trees Minimal Spanning Tree
Can you give a
few spanning
tree?
Azlina Binti Morshidi/JMSK/PKS
Trees
Minimal Spanning Tree using Prim’s Algorithm
Find a minimal spanning tree of the weighted graph below.
Azlina Binti Morshidi/JMSK/PKS
Trees
Minimal Spanning Tree using Prim’s Algorithm
First, arrange the edges by decreasing weights.
Then successively delete the edges without disconnecting graph
until 6 edges remain.
Edges ab eg eb fe bd cd bc gd fg bg
Weight 6 6544 3 2 2 2 1
Delete?
c
ab
ed
f
g
Azlina Binti Morshidi/JMSK/PKS
Trees
Minimal Spanning Tree using Prim’s Algorithm
First, arrange the edges by decreasing weights.
Then successively delete the edges without disconnecting graph
until 6 edges remain. (Remove = 10 edges -7 vertices +1 =4
Edges ab eg eb fe bd cd bc gd fg bg
Weight 6 6544 3 2 2 2 1
Delete?
c
ab
ed
f
g
Azlina Binti Morshidi/JMSK/PKS
Trees
Minimal Spanning Tree using Prim’s Algorithm
First, arrange the edges by decreasing weights.
Then successively delete the edges without disconnecting graph
until 6 edges remain. (Remove = 10 edges -7 vertices +1 =4
Edges ab eg eb fe bd cd bc gd fg bg
Weight 6 6544 3 2 2 2 1
Delete?
c
ab
ed
f
g
Azlina Binti Morshidi/JMSK/PKS
Trees
Minimal Spanning Tree using Prim’s Algorithm
Find the minimal spanning tree from the graph below:
g
Azlina Binti Morshidi/JMSK/PKS
Trees
Minimal Spanning Tree using Kruskal’s Algorithm
Algorithm:
The input is connected weighted graph G with n vertices.
Step 1- Arrange the edges of G in the order of increasing weights
(small-> big)
Step 2- Starting only with the vertices of G and proceeding
sequentially, add edges which does not result in a cycle until n-1
edges added
Formula to added the edges = n - 1
Step 3 - Exit
Azlina Binti Morshidi/JMSK/PKS
Trees
Minimal Spanning Tree using Kruskal’s Algorithm
Find a minimal spanning tree of the weighted graph below.
Azlina Binti Morshidi/JMSK/PKS
Trees
Minimal Spanning Tree using Kruskal’s Algorithm
First, arrange the edges by decreasing weights.
Then successively delete the edges without disconnecting graph
until 6 edges remain.
Edges bg fg gd bc cd bd ef bd eb eg
Weight 1 2223 4 4 456
Add?
c
ab
ed
f
g
Azlina Binti Morshidi/JMSK/PKS
Trees
Minimal Spanning Tree using Kruskal’s Algorithm
First, arrange the edges by decreasing weights.
Then successively delete the edges without disconnecting graph
until 6 edges remain.
Edges bg fg gd bc cd bd ef be ab eg
Weight 1 2223 4 4 566
Add?
c
ab
ed
f
g
Azlina Binti Morshidi/JMSK/PKS
Trees
Minimal Spanning Tree using Kruskal’s Algorithm
Find the minimal spanning tree from the graph below:
g
Azlina Binti Morshidi/JMSK/PKS
Trees
Minimal Spanning Tree
Find the minimal spanning tree from the graph below using Prim’s
algorithm and Kruskal’s Algorithm:
Azlina Binti Morshidi/JMSK/PKS
Trees
Minimal Spanning Tree
Find the minimal spanning tree from the graph below using Prim’s
algorithm and Kruskal’s Algorithm:
Azlina Binti Morshidi/JMSK/PKS
Trees
Binary Search Tree
A binary tree is a root tree in which each vertex has either no
children, one children or two children.
If a vertex has one child, that child is designated as either a left
child or a right child.
If a vertex has two children, one child is designated a left child and
the other child is designated a right child.
Main advantage of binary search trees is rapid structure, which
meets the following requirements;
It is a binary tree.
Each node contains a value.
Every two values can be compared with each other.
Left sub tree of a node contains only values lesser, than the
node’s value.
Right sub tree of a node contains only values greater, than
the node’s value.
Azlina Binti Morshidi/JMSK/PKS
Trees
Binary Search Tree
Vertex b is the Vertex c is the
left child of a
a right child of
vertex a
vertex a
b c
e
Vertex c has no
right child
d
Vertex b has
no child left
fg Vertex e is the
left child of ver
tex c
Azlina Binti Morshidi/JMSK/PKS
Trees
Binary Search Tree
Build a binary search tree for the set of data below;
50 33 44 22 77 35 60 40 55
Misha Kelana Safia Wahidah Syukree Badar Christie
Hockey Football Golf Squash Tennis Futsal Baseball
Volleyball Takraw Basketball Pool
Manggo Pineapple Banana Apple Strawberry Berry Durian
Azlina Binti Morshidi/JMSK/PKS
Trees Binary Search Tree
Build a binary search tree for the set of data below;
50 33 44 22 77 35 60 40 55
50
33 77
22 44 60
35 55
40
Azlina Binti Morshidi/JMSK/PKS
Trees Binary Search Tree
Build a binary search tree for the set of data below;
Misha Kelana Safia Wahidah Syukree Badar Christie
Misha
Kelana Safia
Badar Wahidah
Christie Syukree
Azlina Binti Morshidi/JMSK/PKS
Trees Binary Search Tree
Build a binary search tree for the set of data below;
Hockey Football Golf Squash Tennis Futsal Baseball Volleyball
Takraw Basketball Pool
Hockey
Football Squash
Baseball Golf Pool Tennis
Basketball Futsal Takraw Volleyball
Azlina Binti Morshidi/JMSK/PKS