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

0% found this document useful (0 votes)
61 views70 pages

T3 Graph and Tree

This document defines key concepts related to graphs and trees. It begins by defining a graph as a pictorial representation consisting of vertices and edges. It notes that formally a graph is defined as a pair of sets (V,E) where V is the set of vertices and E is the set of edges. It then defines key graph terminology including vertices, edges, loops, parallel edges, incidence, adjacency, and degree of a vertex. It also defines and provides examples of different types of graphs like simple graphs, multigraphs, pseudographs, directed graphs, weighted graphs, connected graphs, discrete graphs, complete graphs, linear graphs, and bipartite graphs. Finally, it discusses concepts of paths, circuits, cycles, and plan

Uploaded by

Cerealkiller
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)
61 views70 pages

T3 Graph and Tree

This document defines key concepts related to graphs and trees. It begins by defining a graph as a pictorial representation consisting of vertices and edges. It notes that formally a graph is defined as a pair of sets (V,E) where V is the set of vertices and E is the set of edges. It then defines key graph terminology including vertices, edges, loops, parallel edges, incidence, adjacency, and degree of a vertex. It also defines and provides examples of different types of graphs like simple graphs, multigraphs, pseudographs, directed graphs, weighted graphs, connected graphs, discrete graphs, complete graphs, linear graphs, and bipartite graphs. Finally, it discusses concepts of paths, circuits, cycles, and plan

Uploaded by

Cerealkiller
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/ 70

Graph and Tree

Define Graph

Graph theory is the study of graphs that


concerns with the relationship among edges
and vertices.
Define Graph
• A graph is a pictorial representation of a set of objects where
some pairs of objects are connected by links.

• The interconnected objects are represented by points termed


as vertices, and the links that connect the vertices are
called edges.

• Formally, a graph is a pair of sets (V, E), where V is the set of


vertices and E is the set of edges, connecting the pairs of
vertices.
• Edge
• Vertex An edge is the
A vertex is a point mathematical term for a
where multiple lines line that connects two
meet. It is also
called a node. A vertices. Many edges
vertex is denoted by can be formed from a
an alphabet or single vertex. Without a
vertex
numbers. vertex, an edge cannot
.b be formed. There must
. be a starting vertex and
a
edge an ending vertex for an
edge.
Graph Terminology
Basic Terminology
• Parallel Edges
In a graph, if a pair of vertices is connected by more than one
edge, then those edges are called parallel edges. Sometimes it
also called multiple edges.

In the above graph, ‘a’ and ‘b’ are the two vertices which
are connected by two edges ‘ab’ and ‘ab’ between them.
So it is called as a parallel edge.
Basic Terminology
• Loop
In a graph, if an edge is drawn from vertex to
itself, it is called a loop

In the above graph, V is a vertex for which it has an edge (V, V) forming
a loop
Basic Terminology
• Incident
-Such an edge, 1 is called incident with the vertices a and b and
1 is said to connect a and b.

1 2

3 4 5

6 7
Basic Terminology
• Adjacent
-In a graph, two vertices are said to be adjacent (or
neighbour), if there is an edge between the two
vertices. Here, the adjacency of vertices is maintained
by the single edge that is connecting those two
vertices.
Notation − deg(V).
Example : in the graph −
•‘a’ and ‘b’ are the adjacent vertices, as there is a
common edge ‘ab’ between them.
•‘a’ and ‘d’ are the adjacent vertices, as there is a
common edge ‘ad’ between them.
•‘a’ and ‘f’ is not adjacent.
•Can you find the adjacent vertices for e.
Basic Terminology
• Degree of Vertex
• The degree of a vertex in an undirected graph is the number of edges
that are incident on vertex, except the loop at a vertex counted twice.
• Notation − deg(V).
No.of
degree
• A vertex of degree deg(a) 2
zero is called deg(b) 3
isolated. deg(c) 1
• A vertex is a
pendant if and only if deg(d) 2 Pendant
it has a degree one. deg(e) 0
Isolated vertex
Exercise Answer all the question based on graph X.

a 1 b Alphabets are representing the vertices and number


are representing the edges.
7
4 6
c a. Write the vertex set and the edge set
2 3 d
5
8 b. Write:
9 10
e i. edges that are incident on a
g ii. vertices that are adjacent to d
13 12 11
iii. edges that are adjacent to 7
f d iv. loops
v. parallel edges
Graph X vi. isolated vertices.

c. Determine the degree of each vertex.


Graph Terminology: Simple Graph
• A graph in which each edge connects two different vertices
and where no two edges connect the same pair of vertices is
called a simple graph. For example, Consider the following
graph –

A graph with no loops and no parallel edges is called a simple graph.


Graph Terminology: Multigraph
• A graph in which multiple edges may connect the same pair of
vertices is called a multigraph

b c

Any graph which contains some multiple edges


Graph Terminology: Pseudograph
• A pseudograph is a non-simple graph in which both loops and parallel
edges are permitted
Graph Representations
Directed Graph
• A Digraph or directed graph is a graph in which each edge of
the graph has a direction. Such edge is known as directed
edge.
Weighted Graph
• A weighted graph is a graph in which each branch is given a numerical
weight.
Connected Graph
• A connected graph is graph that is connected
• A graph that is not connected is said to be disconnected

Connected graph

Disconnected graph
Different types of graphs
– special simple graph
DISCRETE GRAPH COMPLETE GRAPH LINEAR GRAPH BIPARTITE GRAPH
• A graph that composed of • A complete graph on n • The edge that connect at least • A simple graph G is called bipartite if its
isolated vertex only called as vertices, denoted by Kn , is two vertices. Denoted by Ln . vertex set V can be partitioned into two
disjoint sets V1 and V2 such that every
discrete graph, which the simple graph that edge in the graph connects a vertex in
denoted Dn, where n is equal contains exactly one edge V1 and a vertex in V2.
to the number of vertices. between each pair of distinct
vertices. • It cannot be an edge in G connects
either two vertices in same vertex set.
• We call the pair (V1, V2) a bipartition of
the vertex.
• The complete bipartite graph, denoted
Km,n is the graph that has its vertex set
partitioned into two subsets of m and n
vertices, respectively
PATHS, CYCLES AND
PLANARITY
PATHS

A path is a
sequence of edges
connecting vertices
in a graph. Start
and end with
different vertex.

Simple path: [ 1, 2, 4, 5 ]
CIRCUIT/CYCLES

The path is called


as circuit if it
begins and ends at
the same vertex.

Circuit: [ 1, 2, 4, 5, 4, 1]
PLANAR GRAPHS
• A graph is called planar if it can be drawn in the plane without any
edges crossing.
• A planar graph split the plane into regions, including an unbounded
region.
• Theorem: EULER’S FORMULA: Let G be the connected planar simple
graph with e edges and v vertices. Let r be the number of regions in a
planar representation of G. Then r = e – v + 2.
PLANAR GRAPHS
• Example :
Identify whether the K4 is planar or not.

Steps :
1. Calculate number of region that the K4 should have by using the
formula r = e- v + 2
2. Draw the planar with the region.
3. State it is planar or not.

1. r=e-v+2 2. 3. K4 is a planar graph as G1


and G2 is a planar
=6-4+2
=4
ISOMORPHIC
• A graph can exist in different forms having the same number of
vertices, edges, and also the same edge connectivity. Such graphs
are called isomorphic graphs.
Two graphs G1 and G2 are isomorphic if there exists a matching between
their vertices so that two vertices are connected by an edge in G1 if and only
if corresponding vertices are connected by an edge in G2.

• A property preserved by isomorphic graphs are:-


• Must have the same number of vertices.
• Must have the same number of edges.
• The degrees of the vertices must be the same.
• Both graph is a bijective mapping.
ISOMORPHIC
Example :
Show the isomorphic between graph G1 and G2.

G1 G2
ISOMORPHIC
Answer G1 G2

Properties Graph G1 Graph G2


No. of vertices 5 5
No. of edges 5 5
No. of degree deg(a) = 2 deg(p) = 2
deg(b) = 2 deg(q) = 2
deg(c) = 2 deg(r) = 2 Thus, G1 and G2 are
deg(d) = 2 deg(s) = 2 Isomorphic.
deg(e) = 2 deg(t) = 2
Vertices in G1 a=p
correspond with the b=q
vertices in G2 (Bijective c=r
mapping) d=s
e=t
ISOMORPHIC
Properties Graph G Graph H
• Example : Determine whether the No. of vertices 8 8
• graph G and the graph H are No. of edges 10 10
No. of degree deg(a) = 2 deg(s) = 3
• isomorphic. deg(b) = 3 deg(t) = 2
deg(c) = 2 deg(u) = 2
deg(d) = 3 deg(v) = 3
deg(e) = 2 deg(w) = 3
deg(f) = 3 deg(x) = 2
deg(g) = 2 deg(y) = 2
deg(h) = 3 deg(z) = 3
Vertices in G1 However, G and H are not
correspond with the isomorphic because there are no
vertices in G2 vertices in H correspond with the
(Bijective mapping) vertices in G.
PATHS AND CIRCUIT
• Path is a sequence of edges that begins at a vertex of a graph and
travels from vertex to vertex along edges of the graph.
• The path is called as circuit if it begins and ends at the same vertex.
• A path or circuit is simple if it does not contain the same edge more
than once.
EXERCISE : PATH,CIRCUIT
a e1 b

e2 Answer all the question based on graph Y.

i. Write the path from a to f


c d
e3 ii. Write the path from b to d
iii. Write the circuit start at c
e5 e4 iv. Write the circuit start at e
e6

e f

Graph Y
EULER PATHS AND EULER CIRCUIT
• An Euler path is a path that starting and ending at the
difference vertices and visits each edge only once.
• Theorem 1: A connected graph has an Euler path but not an
Euler circuit if and only if it has exactly two vertices of odd
degree
• An Euler circuit is a circuit that starting and ending at the same
vertex and visits each edge only once.
• Theorem 2: A connected graph with at least two vertices has an
Euler circuit if and only if each of its vertices has even degree.
EXERCISE
• Determine which of the following graph has the Euler path. If it
has, write the path.
EXERCISE
• Determine which of the following graph has the Euler circuit. If it
has, write the circuit.
HAMILTON PATHS AND EULER CIRCUIT
• Hamilton path is a path that starting and ending at the
difference vertices and passes through every vertex exactly
once.
• Hamilton circuit is a circuit that starting and ending at the
same vertex and passes through every vertex exactly once.
• A graph with a vertex of degree one cannot have a Hamilton
circuit.
EXERCISE
Determine which of the simple graph below have a Hamilton circuit or,
if not, a Hamilton path?
TRAVELLING SALESMAN PROBLEM (TSP)
• The Travelling Salesman Problem is related to the problem of finding a
Hamiltonian circuit in graph.
• The problem is given a weighted graph G, find a minimum length
Hamiltonian curcuit in G.
• Consider the following problem : A travelling salesperson wants to
visit each of n cities exactly once and return to his starting point.
• Idea to solve the TSP :
• List all possible Hamiltonian circuits
• Find the length of each circuit by adding the edge weights
• Select the circuit with minimal total weight.
EXAMPLE
• 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 V1.

Shotest distance = V1 – V2 –V4 – V6 – V5 – V3 – V1


= 5 + 3+ 7 +7 + 6 + 6
= 34
EXAMPLE
• The director of an art museum is planning to send out an exhibit to three area schools.
She wants to minimize travel time. The exhibit must travel to each of the three schools
once on a particular day and return to the museum after visiting each of the three schools.
The travel times, in minutes, between the schools and between the museum and the
schools is given in the graph.

Minimum time = Museum - School B - School C – School A - Museum


= 49 + 36 + 33 + 53
= 171 minutes
TREE
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.
Example of tree
Example of not tree
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.
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.
TREE TERMINOLOGY
• Root :node without parent(a)
• Siblings : nodes share the same parent
• Internal node : node with at least one child (a, b, c)
• External node (leaf) : node without children (f,g,e,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 (2)
• 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
EXERCISE

Determine the : e) Descendents of 3?


a) Level of 10 f) Ancestors of 18?
b) Height of this rooted tree g) Sibling of 12?
c) Children of 6 h) Leaf?
d) Parent of 20 i) Internal Vertex?
SPANNING TREE
• A spanning tree of a graph with n vertices is a subset of n – 1 edges that
form a tree. For example, the spanning trees of the cycle graph, diamond
graph and complete graph are illustrated below.
MINIMAL SPANNING TREE
• Minimal spanning in a connected weighted graph is a spanning tree
that has the smallest possible sum of weight of its edges.
MINIMAL SPANNING TREE
PRIM’S ALGORITHM
Step 1
Select any vertex, and choose the edge and smallest weight
from G.
Step 2
At each stage, choose the edge of smallest joining a vertex
already included to vertex not yet included.
Step 3
Continue until all vertices are included.
Example
Example
Example
Example
Hence,
EDGE WEIGHT
1 AD 5
2 DF 6
3 AB 7
4 BE 7
5 EC 5
6 EG 9
MINIMAL SPANNING TREE
KRUSKAL’S ALGORITHM

Step 1
Find the edge in the graph with smallest weight (if there is more than one, pick
one at random). Mark it with any given color, say red.

Step 2
Find the next edge in the graph with smallest weight that doesn’t close a cycle.
Color the edge and the next incident vertex.

Step 3
If G has n vertices, stop after n-1 edges have been chosen. Otherwise repeat.
Example
Example
Example
Hence,
EDGE WEIGHT
1 AD 5
2 CE 5
3 DF 6
4 AB 7
5 BE 7
6 EG 9
EXERCISE
EXERCISE
BINARY SEARCH TREE
BINARY SEARCH TREE
• Build a binary search tree for the set of data below;
• 50 33 44 22 77 35 60 40 55
TREE TRAVERSALS
• Pre-Order
[ root – left – right ]

20 7 3 15 10 19 12 25 22 30 27
TREE TRAVERSALS
• In-Order
[left – root – right ]

3 7 10 15 12 19 20 22 25 27 30
TREE TRAVERSALS
• Post-Order
[left – right –root ]

3 10 12 19 15 7 22 27 30 25 20
FULL AND COMPLETE BINARY TREE

Full Binary Tree Complete Binary Tree


TRAVELLING SALESMAN PROBLEM SOLVING

The Traveling Salesman Problem (TSP) is a challenge to the salesman


who wants to visit every location exactly once and return home, as
quickly as possible. Each location can be reached from every other
location, and for each pair of locations, there is metric that defines the
time between them.
TRAVELLING SALESMAN PROBLEM SOLVING

DEFINITION
Given a set of cities and the cost of travel (or distance) between each
possible pairs, the TSP, is to find the best possible way of visiting all the
cities and returning to the starting point that minimize the travel cost (or
travel distance).
EXAMPLE
A communication in company is in the process of identifying required cost to updates all paths
between its station. A weighted graph below shows the stations and the costs in millions of dollars
for upgrading each link. Use Kruskal’s Algorithm to find the minimum cost needed for upgrading
these links.

You might also like