Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CH - 6 & 7 Graphs and Trees

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Non linear Data Structures

Graphs and Trees

1
Tree
A Tree is also a mathematical non-linear data structure, but it organizes
the data in the context of hierarchy, i.e. the data is represented by a
node and the successive data is denoted by the nodes just below it
which is termed as the child node.

Trees are used to arrange the data in a sorted order because they take a
hierarchical structure between data elements.

A Tree arranges the data into branches that relate the information and
properties.

When a new edge is added to a tree, it creates loop or circuit.


The important applications of a tree include file storage, data
compression, arithmetic expression manipulation, etc.
Non linear Data Structures 3
5
CS 11001 : Programming and Data Structures 6
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. Take a look at
the following graph −
here, V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}
In computer programming, a Graph is a non-linear data structure which
consists of vertices (also known as nodes) and edges to show the relation
between data.

The nodes on a graph are the circles or points, while the edges are the
line segments or arcs.

Graphs are basically the mathematical non-linear data structure that are
used for representation of different kinds of physical structures.

In a graph, a node can have any number of edges. But, it does not have a
unique node like a root.

Also, in a graph, a cycle could be formed.


Graphs are used in different applications such as finding shortest path in
a networking graph, computer networks, electric circuits, etc.
Mathematical graphs can be represented in data structure. We can
represent a graph using an array of vertices and a two-dimensional array
of edges. Before we proceed further, let's familiarize ourselves with some
important terms −

•Vertex − Each node of the graph is represented as a vertex. In the


following example, the labeled circle represents vertices. Thus, A to G
are vertices. We can represent them using an array as shown in the
following image. Here A can be identified by index 0. B can be identified
using index 1 and so on

•Adjacency − Two node or vertices are adjacent if they are connected to


each other through an edge. In the following example, B is adjacent to A,
C is adjacent to B, and so on.
•Edge − Edge represents a path between two vertices or a line
between two vertices. In the following example, the lines from
A to B, B to C, and so on represents edges.

•We can use a two-dimensional array to represent an array as


shown in the following image.

•Here AB can be represented as 1 at row 0, column 1, BC as 1


at row 1, column 2 and so on, keeping other combinations as
0.

•Path − Path represents a sequence of edges between the two


vertices. In the following example, ABCD represents a path
from A to D.
Basic Operations
Following are basic primary operations of a Graph −
Add Vertex − Adds a vertex to the graph.
Add Edge − Adds an edge between the two vertices of the graph.
Display Vertex − Displays a vertex of the graph.
Main Graph Traversal Algorithms
Traversing a graph means exploring its structure by visiting the nodes
according to some systematic rule.

The most effective rule depends on the type of graph and the problem at hand
so it’s not possible to make truly general statements.

Since many data structures in computer science have an underlying graph that
is a tree, it’s worth spending some effort to understand better the strategies to
explore such a graph in order to find paths or nodes that satisfy the conditions
of the problem at hand.

In many cases, the problem can be reduced to a statement such as “find the path
between node A and B such that a certain quantity is optimized”.

The quantity may be for example the number of edges (i.e. the length of the
path), or the sum of a certain feature (possibly binary) associated to each node.

Two main approaches are called Depth-First Search (DFS) and Breadth-First
Search (BFS).
Depth First Traversal
Depth First Search (DFS) algorithm traverses a graph in a depthward
motion and uses a stack to remember to get the next vertex to start a
search, when a dead end occurs in any iteration.

As in the example given above, DFS algorithm traverses from S to A


to D to G to E to B first, then to F and lastly to C. It employs the
following rules.
•Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display
it. Push it in a stack.
•Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack.
(It will pop up all the vertices from the stack, which do not have
adjacent vertices.)
•Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
Breadth First Traversal
Breadth First Search (BFS) algorithm traverses a graph in a breadthward
motion and uses a queue to remember to get the next vertex to start a
search, when a dead end occurs in any iteration.

As in the example given above, BFS algorithm traverses from A to B to E to F first


then to C and G lastly to D. It employs the following rules.
•Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited.
Display it. Insert it in a queue.
•Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
•Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
ANY QUESTION?

You might also like