• No results found

16 Java

N/A
N/A
Protected

Academic year: 2020

Share "16 Java"

Copied!
15
0
0

Loading.... (view fulltext now)

Full text

(1)

G. ANKAMMA RAO

Trees and Graphs

About Trees

Applications of Trees

About Graphs

Applications of Graphs

(2)

Trees and Graphs

Tree

It is defined as finite set of elements. These elements are stored in the form of nodes. There would be

one node under which the elements would be present. That node is termed as root node. The root node has

various nodes present under it. These nodes are called as child nodes. A tree can contains any number of

Childs. There is no restriction in number of child’s.

Ex:

Binary Tree

It is a tree with a finite set of nodes which is either empty or consists of a root with two sub trees i.e.,

left sub tree and right sub tree. For example consider the following tree.

Chapter

(3)

Root node

FIGURE: BINARY TREE

Here A is a root node, B and C are the Childs of A i.e., A is a parent of B and C. Here B and C are

called as Siblings.

Sibling is nothing but two Childs contains the same parent. Here B and C contain the parent A.

Therefore, Binary tree contains zero children or one child or twochildren’s. Trees concept can be implemented

by using Double linked list.

Leaf Node

A node with zero children’s is called as a leaf node. In the above tree (example of binary tree) the

nodes D, H, I and G are called as leaf nodes.

Level of a Tree

Root node is also termed as parent node. The root node is assigned to level 0.The numbering of level starts

(4)

From the above tree

 Level of A is 0.

 Level of B and C is 1.

 Level of D, E, F and G is 2.

 Level of H and I is 3.

The maximum level of a tree is called as Depth of the tree.

Strictly Binary Tree

It is a binary tree; each node can contain 0 or two children.

Example

The strictly binary tree with n leaf nodes always contains 2n-1 nodes.

Complete Binary tree

It is aBinary tree with each node contains zero children’s or two children’s and also each leaf node

has to lie in the same level.

(5)

Applications of Trees

1. Tree Traversals

One of the important feature of a Binary tree is tree traversal. Tree traversal means processing each

node exactly once without leaving any node. This is used to point the node values in a binary tree. There are

three types of the transversals in a tree. They are

 In-Order (left, root, right).

 Pre-Order (root, left, right)

 Post-Order (left, right, root).

In Order:

1. Traverse the left sub tree of root R in In Order.

2. Traverse the root node R

3. Traverse the Right sub tree of root R in In Order

Pre Order:

1. Process the root R.

2. Traverse the left sub tree of root R in Pre Order.

3. Traverse the Right sub tree of root R in Pre Order.

Post Order:

1. Traverse the left sub tree of root R in Post Order.

2. Traverse the Right sub tree of root R in Post Order.

3. Traverse the root R.

For example, consider thefollowing binary tree…..

In-Order: B, A, C

Pre-Order: A, B, C

Post-Order: B, C, A

A A

(6)

Consider a binary tree

In-Order: D, B, E, A, F, C, G

Pre-Order: A, B, D, E, C, F, G

Post-Order: D, E, B, F, G, C, A

2. Expression trees:

A tree is constructed by using infix or postfix expression is called Expression tree.

Constructing expression tree from infix expression:

In this select middle operator as root for expression tree, left and right operands are inserted as left

child and right child for the root.

Ex: A + B

Expression tree for the above infix expression

Ex: (A + B) * (C - D)

Expression tree for the above infix expression.

B A

+

* + A B - C

+ A B D A C B G

(7)

3. Binary Search Tree

Binary Search tree is a binary tree in which each internal node x stores an element such that the element

stored in the left sub tree of x are less than or equal to x and elements stored in the right sub tree of x are

greater than or equal to x. This is called binary search-tree property.

Example: 50, 40, 35, 65, 55, 45, 70, 68, 66

GRAPHS

A graph is nothing but set of nodes or vertices and set of lines or edges or arcs that connect two vertices.

More formally, we define a graph G as an ordered pair G = (V,E) where

 V is a set of nodes (vertices).

 E is a set of edges (links).

 Each edge is a pair of vertices. In other words, each element of E is a pair of elements of V. +

+

(8)

Example: The picture above represents the following graph:

 V = {1,2,3,4,5,6}

 E = {{1,2},{1,5},{2,3},{2,5},{3,4},{4,5},{4,6}}

 G = (V,E)

Undirected Graph

A Graph that does not contain any direction is called as undirected graph.

Directed Graph

A pair of vertices between the edges must be ordered then that graph is known as directed graph.

The following figure gives a brief description about the both direct and undirected graphs.

Loop

In a directed graph some edges may connect to itself then such type of edges are called as a loop. This

(9)

In the above figure, (A, A) is a loop.

Path

A path in a graph represents a way to get from an origin to a destination by traversing edges in the

graph. For example, in the undirected graph G=(V,E) drawn below, there are many paths from node 6 to node

1. One such path is highlighted with red:

We write a path P as an ordered list of directed edges: P = ((v1,v2),(v2,v3),...,(vk,vk+1)). The first vertex

of the first edge of a path is the origin and the second vertex of the last edge is the destination. Both origin and

destination are called endpoints of the path.

Examples:

 The red path above is ((6,4), (4,3), (3,2), (2,5), (5,1)); it is a path in G from node 6 to node 1

 The blue path below is ((6,4), (4,5), (5,1)); it is also a path in G from node 6 to node 1

Weighted Graph

Some graphs contain weights on their edges, such type of graphs is called as weighted graph and the

number associated with an edge is called as weight.

(10)

General Example for a Graph:

A graph is nothing but set of bus stops as vertices and edges are nothing but the bus route between the

bus stops and weight is nothing but the distance between the bus stops in kilometers.

In-degree of a node

The In-degree of a node n is nothing but the number of edge coming to that node. For example consider

the following figure,

In this the In-degree of node A is 0, in-degree of node B is 2 and the in-degree of node C is 2.

Out-Degree of a node

The out-degree of a node n is nothing but the number of edges that are move away from that node.

Consider the following graph,

In this the out-degree of node A is 2, out-degree of node B is 1 and the out-degree of node C is 1.

Isolated Node

In a graph if a node that does not adjacent to any other node then that node is called as an isolated node.

Example:

(11)

Cyclic Graph

A path from a node to itself is called as a cyclic graph. In other words, a graph containing a cycle in it

is called as a cyclic graph.

Example:

Acyclic Graph

The graph does not contain the cycle is called as an acyclic graph and the directed graph that does not

contain any cycle is called as directed acyclic graph. This is shown in below.

Graph Traversals

Graph traversal means visiting each vertex in a graph without leaving any vertex. There are two ways

to traverse a graph.

 DFS (Depth First Search).

 BFS (Breadth First Search).

DFS (Depth First Search)

Consider a graph G and its starting vertex is V. In this DFS, first we visit the vertex V after that we

visit the adjacent vertices of V.

For example consider a graph G with the starting vertex V and its adjacent vertices w1, w2, w3, ….. wk.

Therefore in DFS, first we visit the vertex V after that we visit the vertex w1. After visiting the vertex w1we

visit the adjacent vertices of w1in depth first manner before visiting w2, w3, ….wk. The search is completed

once all the nodes are processed.

(12)

Let us consider the starting vertex as 1, then the DFS traversal is : 1, 2, 4, 8, 5, 3, 6, 7

Another possible DFS traversal is: 1, 3, 6, 8, 7, 2, 4, 5

BFS (Breath First Search)

It is different from DFS. Here the starting vertex V is visited first then the adjacent vertices of V are

visited in the next step, then after that the adjacent vertices of those nodes are traversed and so on. This process

will be continued until all the nodes are processed.

This algorithm uses the queue and stores vertices of each level when they are visited. They had taken

out one by one when their adjacent vertices are processed. This process is continued until queue is empty.

For example consider the following undirected graph,

(13)

Applications of Graphs

Minimum Spanning tree

The sum of weights of edges of tree as small as possible then it is called as the minimum spanning

tree.

Spanning tree: It is a tree it contains all the nodes in the graph.To find minimum spanning tree we use Prism’s

Algorithm.

Prism’s Algorithm:

Consider the graph ‘G’. Initially the vertices of graph contains the nodes in spanning tree and it edges

not contain edges. In the next step we add minimum cost edge from the starting vertex. And then we add an

edge with minimum cost adjacent to any node in the trees. This process is repeated until all the nodes are

connected. We ensured that whenever we are adding an edge it does not contain any cycle in the spanning

tree.

Consider the following graph:

Let vertex 1 is the starting vertex.

(14)

Step 3: Add an edge from vertex 1 to 2 because vertex 2 is adjacent to vertex 1 and also the minimum cost.

Step 4: Add an edge from vertex 4 to 3.

Step 5: Add edge from vertex 4 to7.

(15)

Step 7: Add edge from vertex 7 to 5.

Figure

Fig:  Complete Binary Tree

References

Related documents

Ten years into the reign of Thorin the First, son of Thráin the Old, the King resolved to remove the royal house of Durin’s folk from Erebor to abide in the Grey Mountains.. At the

To give you the debugging power you need, each oscilloscope comes standard with advanced features including sophisticated triggering, automatic measurements, digital filtering,

One circuit rotates the solar panel in east to west direction and other circuit is used to rotate solar panel in circular direction.. The above circuit is very basic

Nesse sentido, por meio da análise das crenças de eficácia de professores de Física, discu- timos a relação entre a formação acadêmica de professores dessa disciplina e as-

Finally, the Fabric scripts and configuration files can be modi- fied so that the software included in the VM is tailored to the data analysis needs of each researcher, while

Moreover, in question 11(Do you think you learn better, less or the same with JClic programme?), 94’7% of the students think JClic helps them to understand the English language

One practical base for measuring administra- tive costs is the number of potential beneficiaries -for HI the total number (including both insured a11tl uninsured)