Trees & Graphs
Lecture 04
Todays Agenda
Data Structure
Tree Terminologies
Graph Terminologies
2
Data Structure
• Data Structure is a way to store and organize data so that it
can be used efficiently.
• There are many ways of organizing the data in the memory.
• Data Structure is a set of algorithms that we can use in any
programming language to structure the data in the memory.
3
Classification of Data Structures
4
Non-Linear Data Structures
• Non-Linear Data Structures are data structures where the data elements
are not arranged in sequential order.
• Here, the insertion and removal of data are not feasible in a linear
manner.
• There exists a hierarchical relationship between the individual data
items.
• Tree and Graph are some example of Non-Linear data structure.
5
Tree
• Tree is a Non-Linear Data Structure containing a collection of nodes
such that each node of the tree stores a value and a list of references to
other nodes (the "children").
• Tree data structure is a method to arrange and collect data in the
computer to be utilized more effectively.
• We can say that the tree data structure consists of roots, branches, and
leaves connected.
6
Tree
7
Some Applications of Trees
1. Trees implement hierarchical structures in computer systems like
directories.
2. Trees are also used to implement the navigation structure of a website.
3. Trees are helpful in decision-making in Gaming applications.
4. Trees are responsible for implementing priority queues for priority-
based OS scheduling functions.
5. Trees are also responsible for parsing statements in the compilers.
6. Trees are used in the path-finding algorithm implemented in Artificial
Intelligence and Robotics.
8
Tree Terminology
Root Node:
• The topmost node in a tree structure, serving as the starting
point for traversal.
• The root node serves as the starting point for decision-making
or problem-solving in AI.
• It represents the initial state or question that needs to be
addressed.
9
Tree Terminology
Parent Node:
• A node that has branches or children is known as Parent Node.
Purpose in AI:
• Parent nodes in decision trees represent intermediate steps or
conditions that help in making decisions.
• They guide the AI system toward different paths based on the
data.
10
Tree Terminology
Child Node:
• A node connected to a parent node, forming branches of the tree.
Purpose in AI:
• Child nodes are the outcomes or options that result from decisions
made at parent nodes
11
Tree Terminology
Leaf Node:
• A node with no children, located at the end of a branch.
Purpose in AI:
• Leaf nodes in decision trees often represent final outcomes or
classifications.
• They provide the results or conclusions of a decision-making
process.
12
Tree Terminology
Internal Node: A node with at least one child.
Depth and Height:
• Depth refers to a node's distance from the root node, while height refers
to the length of the longest path from a node to a leaf node.
Purpose in AI:
• The depth and height of a tree affect the complexity and efficiency of
algorithms used in AI.
• A balanced tree can lead to faster search and decision processes.
13
Tree Terminology
Binary Tree:
• A tree in which each node has at most two children that is left
and right.
14
Depth (Level)
• The depth of a node refers to its distance from the root node of the
tree.
• The root node is considered to be at depth 0.
• Child nodes of the root node have a depth of 1, their children have a
depth of 2, and so on.
• Essentially, the depth of a node represents the number of edges in the
path from the root to that node
15
Height
• The height of a node is the length of the longest path from that node to a
leaf node in the tree.
• The height of a node can be defined as the number of edges on the longest
downward path from that node to a leaf.
• Leaf nodes have a height of 0 because they don't have any children.
• The height of the entire tree is often defined as the height of the root node.
16
Tree Terminology
A
“A, B, C …. J” are “nodes”
B C
“A” is the “root node”
D E F G H
“B” is a “child” of “A”
“A” is ancestor of “D” I J
“D” is a descendant of “A”
“D, E, F, G, I, J” are “leaf nodes”
Arrows represent “edges”
Graph
• Graph is a Non-Linear Data Structure comprising a finite number of
nodes or vertices and the edges connecting them.
• The Graphs are utilized to address problems of the real world such as
social networks and telephone networks.
• For instance, the nodes of a Graph can represent a single user in a
telephone network, while the edges represent the link between them via
telephone.
18
Graph
19
Some Applications of Graphs
1. Graphs represent routes and networks in transportation, travel, and
communication applications.
2. Graphs are used to display routes in GPS.
3. Graphs represent the interconnections in social networks and other
network-based applications.
4. Graphs are also used in robotic motions and neural networks.
20
Graph Terminology
Vertex (Node):
• A point in a graph that can hold data or attributes.
Edge:
• A connection between two vertices, representing a relationship.
Path:
• A sequence of vertices connected by edges in a graph.
21
Graph Terminology
Cycle:
• A path that starts and ends at the same vertex, passing through other
vertices in between.
Shortest Path:
• The path between two vertices with the minimum sum of edge
weights/traversal
Graph Traversal:
• The process of visiting all vertices in a graph, often used to solve search
and optimization problems..
22
Graph Terminology
Graph Purpose in AI:
• Graphs are used in AI for modeling relationships and connections
between data points.
• They are used in various algorithms, like representing knowledge graphs
or social networks.
23
Thank You…!