Graph & Trees-Basic Concept
Graph & Trees-Basic Concept
1
3
2 5
4
• Mathematically, we often write G = (V,E)
V: set of vertices, so |V| = number of vertices
E: set of edges, so |E| = number of edges
Graphs
• A graph G (sometimes called networks) consists of
two things:
(i) A set V = V(G) whose elements are called vertices,
points, or nodes of G.
(ii) A set E = E(G) of unordered pairs of distinct
vertices called edges of G.
• Vertices “u” and “v” are said to be adjacent if there is
an edge e = {u, v} joining them.
• the edge “e” is said to be incident on each of its
vertices u and v.
3
Example of a graph
4
Examples
6
Various types of graphs
• Connected/disconnected graphs
5
0 -2
9
Complete, regular and bipartite graphs
• A graph G is said to be complete if every vertex in G
is connected to every other vertex in G.
A graph G is regular of degree k or k-regular if every
vertex has degree k. (I.e every vertex has equal
number of edges)
• A graph G is said to be bipartite if its vertices V can
be partitioned into two subsets M and N such that
each edge of G connects a vertex of M to a vertex of
N.
• By a complete bipartite graph, we mean that each
vertex of M is connected to each vertex of N.
10
Graphs.G1
Graphs.G2
14
Directed graphs
15
Degrees of digraph
• The outdegree of a vertex v of G, written outdeg(v), is
the number of arcs beginning at v, and the indegree of
v, written indeg(v), is the number of arcs ending at v.
each arc begins and ends at a vertex.
• Theorem: The sum of the outdegrees of the vertices
of a digraph G equals the sum of the indegrees of the
vertices, which equals the number of edges in G.
16
Adjacency matrix of digraph
X Y Z W
No. of 1’s in A is X
equal to no. of Y
edges, row total Z
and col. Gives the W
indegree and
outdegree, resp. 17
Adjacency list
In graph theory, an adjacency list is the representation of all edges or arcs in
a graph as a list.
Connectivity of digraphs
• There are three types of connectivity in a directed graph G:
• (i) G is strongly connected or strong if, for any pair of vertices
u and v in G, there is a path from u to v; and a path from v to
u, that is, each is reachable from the other.
• (ii) G is unilaterally connected or unilateral if, for any pair of
vertices u and v in G, there is a path from u to v or a path from
v to u, that is, one of them is reachable from the other.
• (iii) G is weakly connected or weak if there is a undirected
path between any pair of vertices u and v in G.
19
Digraphs and relations
• The relation R is reflexive if every node has a loop.
• The relations R is symmetric if arcs are bidirectional.
• The relation R is transitive if for any sequence of
consecutive arcs, there is a single arc from the first to
the last node.
20
Digraphs and relations
21
Paths and connectivity
• A path in a graph G consists of an alternating sequence
of vertices and edges of the form;
v0, e1, v1, e2, v2, ………., en-1, vn-1 , еn, vn
• where each edge ei contains the vertices vi-1 and vi
(which appear on the sides of edge ei in the sequence).
• The path is said to be closed if v0 = vn. Otherwise, we say
the path is from v0 to vn, or between v0 and vn, or
connects v0 to vn. A simple path is a path in which all
vertices are distinct.
• Length of a path is the number of edges in the path.
• A graph G is connected if there is a path between any two
of its vertices
22
Trees
Family Tree
Q uresh
G halib M uh arib
T aiem Luayy
A uf Ka'ab A m ir H irs
H u sa is S ehm M urrah Ja m ha A di
Z ah ra Q usayy
M uham m ad
(PBUH)
Tree - Definition
A tree is a finite set of one or more nodes such that:
1. There is a specially designated node called the root.
2. The remaining nodes are partitioned in n 0 disjoint sets T1, T2, …, Tn, where each of
these sets is a tree.
3. T1, T2, …, Tn are called the sub-trees of the root.
Family Tree
Sub-trees
Q uresh root
G halib M uh arib
T aiem Luayy
A uf Ka'ab A m ir H irs
H u sa is S ehm M urrah Ja m ha A di
M uham m ad
(PBUH)
Tree
B F K
C H D L X
Q G M I N P
Node Types
A
B F K
C H D L X
Q G M I N P
B F K
C H D L X
Q G M I N P
Degree 1 Degree 3
Degree 0 Degree 2
Level of a Node
Distance from the root
A
B F K
C H D L X
Q G M I N P
Level 0 Level 2
Level 1 Level 3
A A
B K B
C H L X C
Q M P Q
M
Properties of Binary Trees
• The maximum number of nodes on level i of a binary
tree is 2i
• The maximum number of nodes in a binary tree of
height k is 2k – 1
Full Binary Tree
A binary tree of height k having 2k – 1 nodes is called a full
binary tree
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Complete Binary Tree
A binary tree that is completely filled, with the possible
exception of the bottom level, which is filled from left to right, is
called a complete binary tree
2 3
4 5 6 7
8 9 10 11 12
Binary Tree Traversal
In Order Traversal (LNR)
B K
C H L X if (t) {
InOrder(t->left);
Q M P
visit(t);
InOrder(t->right);
Q C M B H A L K X P }
Binary Tree Traversal
Pre Order Traversal (NLR)
B K
C H L X if (t) {
visit(t);
Q M P
PreOrder(t->left);
PreOrder(t->right);
A B C Q M H K L X P }
Binary Tree Traversal
Post Order Traversal (LRN)
B K
C H L X if (t) {
PostOrder(t->left);
Q M P
PostOrder(t->right);
visit(t);
Q M C H B L P X K A }
Binary Tree Traversal
A
B K
C H L X
Q M P
+ /
A * D *
B C E F
LNR: A+B*C-D/E*F
NLR: -+A*BC/D*EF
LRN: ABC*+DEF*/-
BinaryTree ::~ BinaryTree();
Which Algorithm?
LRN