Sunteți pe pagina 1din 36

Trees

SMP
● A tree is a connected undirected graph with no
simple circuits
● Since a tree cannot have a simple circuit, a
tree cannot contain multiple edges or loops.

SMP
● Which of the graph are trees ?

G2 G3 G4
G1
SMP
● If v is vertex in T other than the root, the
parent of v is the unique vertex u such that
there is a directed edged from u to v.
● When u is the parent of v, v is called child of u.
● Vertices with the same parent are called
siblings.
● The ancestors of a vertex other than the root
are the vertices in the path form the root to
this vertex.
SMP
● The descendants of a vertex v are those
vertices that have v as an ancestor.
● A vertex of tree is called a leaf if it has no
children.
● Vertices that have children are called
internal vertices.
● The root is an internal vertex unless it is the
only vertex in the graph, in which case it is a
leaf
SMP
SMP
Theorem
● A tree with n vertices has n-1 edges
solution : Suppose G is a tree with n vertices.
It is a connected graph.
● We prove that it has (n-1) edges by induction
on n.
● If n=1, it is certainly true,vertex has no edges.
Assume that it is true for n=1,2...(n-1)
● Since every edge is a bridge, the subgrah G1
obtained from G after deleting the edge will
have two components G1 and G2 with n1 and
n2 vertices respectively, where n1+n2=n
SMP

● By the induction hypothesis the number of
edges in both the components together is
(n1-1)+(n2-1)=(n-2)
● Thus number of edges in G will be
(n-2)+1= n-1 edges

SMP
● theorem....etc

SMP
Spanning Tree

● Definition:
● 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 spanning tree of G = ( V, E ) is a connect


graph on V with a minimum number of
edges ( |V| - 1).

SMP
● G1 is the Spanning tree of graph G

a b c d a b c

g g
e f e f

G G1

Remove the looping edges {c,g} and {e,f}


SMP
Minimum Spanning Trees

SMP
● A minimum spanning tree in a connected
weighted graph is a spanning tree that has the
samllest possible sum of weights of its edges.

SMP
● 2 minimum spanning tree algorithms

– Prim's Algorithm
– Kruskal's Algorithm

SMP
● Prim’s Algorithm:
● Begin by choosing any edge with smallest
weight and putting it into the spanning tree,
● successively add to the tree edges of minimu
m weight that are incident to a vertex already
in the tree and not forming a simple circuit with
those edges already in the tree,
● stop when (n – 1) edges have been added.

SMP
● procedure Prim ( G : weighted connected
undirected graph with n vertices )
T : = a minimum weight edge
for i : = 1 to n – 1
begin
e : = an edge of minimum weight incident to a
vertex in T and not forming a simple circuit
in T if added to T
T : = T with e added
end { T is a minimum spanning tree of G }

SMP
● Find minimum spanning tree by using prim's
algorithm

SMP
choice Edge weight

1 {A,D} 5

SMP
choice Edge weight

1 {A,D} 5
2 {D,F} 6

SMP
choice Edge weight

1 {A,D} 5
2 {D,F} 6

3 {A,B} 7

SMP
choice Edge weight

1 {A,D} 5
2 {D,F} 6

3 {A,B} 7

4 {B,E} 7

SMP
choice Edge weight

1 {A,D} 5
2 {D,F} 6

3 {A,B} 7

4 {B,E} 7

5 {E,C} 5

SMP
choice Edge weight

1 {A,D} 5
2 {D,F} 6

3 {A,B} 7

4 {B,E} 7

5 {E,C} 5

6 {E,G} 9

Total 39
:
SMP
● Find minimum spanning tree – prim's algorithm

SMP
SMP
Kruskal's algorithm

SMP
SMP
● Find minimum spanning tree – kruskal's algorithm

SMP
choice Edge weight

1 {A,D} 5

SMP
choice Edge weight
1 {A,D} 5

2 {C,E} 5

SMP
choice Edge weight

1 {A,D} 5
2 {C,E} 5

3 {D,F} 6

SMP
choice Edge weight

1 {A,D} 5
2 {C,E} 5

3 {D,F} 6

4 {A,B} 7

SMP
choice Edge weight

1 {A,D} 5
2 {C,E} 5

3 {D,F} 6

4 {A,B} 7

5 {B,E} 7

SMP
choice Edge weight

1 {A,D} 5
2 {C,E} 5

3 {D,F} 6

4 {A,B} 7

5 {B,E} 7

6 {E,G} 9

Total 39
SMP
● Find minimum spanning tree -kruskal's algorithm

SMP
choice Edge weight
1 {b,f} 1
2 {c,d} 1

3 {k,l} 1

4 {c,g} 2

5 {f,j} 2

6 {a,b} 2

7 {a,e} 3

8 {b,c} 3

9 {j,k} 3

10 {g,h} 3

11 {i,j} 3
SMP total 24

S-ar putea să vă placă și