Graph and Trees.pptGraph and Trees.ppt i detailed topic about graph and trees

MohsinAliKhanWaseer 3 views 66 slides Feb 25, 2025
Slide 1
Slide 1 of 66
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66

About This Presentation

Graph and Trees.ppt i detailed topic about graph and trees


Slide Content

1
Muhammad Mohsin Saeed
Graphs and Trees

Simple Graph
Definition
A simple graph G = (V, E) consists of V, a
nonempty set of vertices, and E, a set of
unordered pairs of distinct elements of V
called edges.
2

A simple graph
3
San Francisco
Denver
Los Angeles
New York
Chicago
Washington
Detroit
How many vertices? How many edges?

A simple graph
4
V = { Chicago, Denver, Detroit, Los Angeles,
New York, San Francisco, Washington }
SET OF VERTICES
E = { {San Francisco, Los Angeles}, {San Francisco, Denver},
{Los Angeles, Denver}, {Denver, Chicago},
{Chicago, Detroit}, {Detroit, New York},
{New York, Washington}, {Chicago, Washington},
{Chicago, New York} }
SET OF EDGES

A simple graph
5
San Francisco
Denver
Los Angeles
New York
Chicago
Washington
Detroit
The network is made up of computers and telephone lines
between computers. There is at most 1 telephone line
between 2 computers in the network. Each line operates
in both directions.
These are undirected edges,
each of which connects two distinct vertices, and
no two edges connect the same pair of vertices.

A Non-Simple Graph
Definition
In a multigraph G = (V, E) two or more
edges may connect the same pair of
vertices.
6

A Multigraph
7
San Francisco
Denver
Los Angeles
New York
Chicago
Washington
Detroit
THERE CAN BE MULTIPLE TELEPHONE LINES
BETWEEN TWO COMPUTERS IN THE NETWORK.

Multiple Edges
8
San Francisco
Denver
Los Angeles
New York
Chicago
Washington
Detroit
Two edges are called multiple or parallel edges
if they connect the same two distinct vertices.

Another Non-Simple Graph
Definition
In a pseudograph G = (V, E) two or more edges
may connect the same pair of vertices, and in
addition, an edge may connect a vertex to itself.

9

A Pseudograph
10
San Francisco
Denver
Los Angeles
New York
Chicago
Washington
Detroit
THERE CAN BE TELEPHONE LINES IN THE NETWORK
FROM A COMPUTER TO ITSELF (for diagnostic use).

Loops
11
San Francisco
Denver
Los Angeles
New York
Chicago
Washington
Detroit
An edge is called a loop
if it connects a vertex to itself.

Undirected Graphs
12
pseudographs
simple graphs
multigraphs

A Directed Graph
Definition
In a directed graph G = (V, E) the edges
are ordered pairs of (not necessarily
distinct) vertices.
13

A Directed Graph
14
San Francisco
Denver
Los Angeles
New York
Chicago
Washington
Detroit
SOME TELEPHONE LINES IN THE NETWORK
MAY OPERATE IN ONLY ONE DIRECTION .
Those that operate in two directions are represented
by pairs of edges in opposite directions.

A Directed Multigraph
Definition
In a directed multigraph G = (V, E) the edges are
ordered pairs of (not necessarily distinct) vertices,
and in addition there may be multiple edges.
15

A Directed Multigraph
16
San Francisco
Denver
Los Angeles
New York
Chicago
Washington
Detroit
THERE MAY BE SEVERAL ONE-WAY LINES
IN THE SAME DIRECTION FROM ONE COMPUTER
TO ANOTHER IN THE NETWORK.

Types of Graphs
TYPE EDGES MULTIPLE EDGES LOOPS
ALLOWED? ALLOWED?
Simple graph Undirected NO NO
Multigraph Undirected YES NO
Pseudograph Undirected YES YES
Directed graph Directed NO YES
Directed multigraphDirected YES YES
17

Sub-graph
C
5 is a sub-graph of K
5
18
C
5
K
5

Union-graph
W
5 is the union of S
5 and C
5
19
C
5
W
5
S
5
a
b
c
e
d
a
c
e
a
b
c
e
d
a
b
c
e
d
f
f

Adjacency matrix
20
This graph has 6 vertices
a, b, c, d, e, f. We can
arrange them in that order. d
W
5
a
b
c
e a
c
e
f

Finding the adjacency matrix
21
a b c d e f
d
a 0 1 0 0 1 1
b
c
d
e
f
FROM
TO
There are edges from a to b, from a to e, and from a to f
W
5
a
b
c
e a
c
e
f

Finding the adjacency matrix
22
a b c d e f
d
a 0 1 0 0 1 1
b 1 0 1 0 0 1
c
d
e
f
FROM
TO
There are edges from b to a, from b to c, and from b to f
W
5
a
b
c
e a
c
e
f

Finding the adjacency matrix
23
a b c d e f
d
a 0 1 0 0 1 1
b 1 0 1 0 0 1
c 0 1 0 1 0 1
d
e
f
FROM
TO
There are edges from c to b, from c to d, and from c to f
W
5
a
b
c
e a
c
e
f

Finding the adjacency matrix
24
a b c d e f
a 0 1 0 0 1 1
b 1 0 1 0 0 1
c 0 1 0 1 0 1
d
e
f
FROM
TO
COMPLETE THE ADJACENCY MATRIX . . .
d
W
5
a
b
c
e a
c
e
f

Finding the adjacency matrix
25
a b c d e f
d
a 0 1 0 0 1 1
b 1 0 1 0 0 1
c 0 1 0 1 0 1
d 0 0 1 0 1 1
e 1 0 0 1 0 1
f 1 1 1 1 1 0
FROM
TO
Notice that this matrix is symmetric. That is a
ij = a
ji Why?
W
5
a
b
c
e a
c
e
f

Planar Graphs
A "planar graph" is
 a graph that can be drawn on a flat plane (like a piece of paper)
without any of its edges crossing each other, meaning all edges only intersect at
their endpoints where vertices are located;
 essentially, it's a graph that can be drawn
without edge intersections on a plane.
 
A graph is planar if it can be drawn in the plane without its edges crossing.
K4 is planar
K5 is NOT planar
K3,3 is NOT planar
K2,3 is planar
26

27
Example

Example
A graph may be planar even if it
represents a 3-dimensional object.
28

29
Coloring Graphs
A graph coloring is an assignment of
labels, called colors, to the vertices of
a graph such that no two adjacent
vertices share the same color.

Maps and Colors
Take a look at this map of the US

Maps and Colors
We can color in the states like this

Maps and Colors
This is a “proper” coloring because no two bordering
states are the same color

Maps and Colors
This coloring uses 6 colors; is it possible to color the
map with fewer colors?

Two Colors?
Can we use only two colors?

Two Colors?
We need to choose a color for Pennsylvania, say blue

Two Colors?
Since New York borders PA, we can’t use blue for NY, so
we need a
second color, say red

An Euler path in a graph is a path that travels
along every edge of the graph exactly once. An
Euler path might pass through individual
vertices of the graph more than once.
Euler Graph (pronounced oiler)
A Euler path is a
snowplow problem where
a snow plow needs to plow
every street exactly once
(with NO backtracking).

An Euler circuit (Graph) is a path that
travels along every edge exactly once (no
backtracking) AND starts and ends at the
same vertex.
Start and finish

Tree Terminology

dog
cat wolf
canin
e
A tree consists of a collection of elements or
nodes, with each node linked to its successors

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or
nodes, with each node linked to its successors
The node at the
top of a tree is
called its root

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or
nodes, with each node linked to its successors
The links from a
node to its
successors are
called branches

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or
nodes, with each node linked to its successors
The successors of
a node are called
its children

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or
nodes, with each node linked to its successors
The predecessor
of a node is
called its parent

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or
nodes, with each node linked to its successors
Each node in a tree
has exactly one
parent except for
the root node,
which has no
parent

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or
nodes, with each node linked to its successors
Nodes that have
the same parent
are siblings

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
A node that has
no children is
called a leaf
node

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
A node that has
no children is
called a leaf
node
Leaf nodes also
are known as
external nodes,
and nonleaf
nodes are known
as
internal nodes

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
A generalization of the parent-child
relationship is the
ancestor-descendant relationship

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
dog is the
parent of cat
in this tree
A generalization of the parent-child
relationship is the
ancestor-descendant relationship

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
cat is the
parent of
canine in this
tree
A generalization of the parent-child
relationship is the
ancestor-descendant relationship

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
canine is a
descendant of
cat in this tree
A generalization of the parent-child
relationship is the
ancestor-descendant relationship

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
dog is an
ancestor of
canine in this
tree
A generalization of the parent-child
relationship is the
ancestor-descendant relationship

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
A subtree of a
node is a tree
whose root is a
child of that
node

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
A subtree of a
node is a tree
whose root is a
child of that
node

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
A subtree of a
node is a tree
whose root is a
child of that
node

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
The level of a
node is
determined by
its distance from
the root

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
The level of a
node is its
distance from
the root plus 1
Level 1
Level 2
Level 3

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
The level of a
node is defined
recursively
Level 1
Level 2
Level 3

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
The level of a
node is defined
recursively
Level 1
Level 2
Level 3
•If node n is the root of tree T, its level
is 1
•If node n is not the root of tree T, its
level is
1 + the level of its parent

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
The height of a
tree is the
number of
nodes in the
longest path
from the root
node to a leaf
node

Tree Terminology (cont.)

dog
cat wolf
canin
e
A tree consists of a collection of elements or nodes,
with each node linked to its successors
The height of a
tree is the
number of
nodes in the
longest path
from the root
node to a leaf
node
The height
of this tree
is 3

62
Tree traversal is a popular form of graph
traversal in the world of computer science
and data structures, and it is a procedure of
visiting each node of the tree. The
sequence in which the nodes are visited is
preferred to classify these traversals.
Tree Traversal

63

64

65

66
END OF COURSE
Tags