Professional Documents
Culture Documents
TREE-AND-GRAPH-DATA-STRUCTURE
TREE-AND-GRAPH-DATA-STRUCTURE
TREE-AND-GRAPH-DATA-STRUCTURE
A tree is an abstract model of a hierarchical structure that consists of nodes with a parent-child relationship.
Tree is a sequence of nodes
There is a starting node known as a root node
Every node other than the root has a parent node.
Nodes may have any number of children
The first subset contains a single element called the root of the tree.
The other two subsets are themselves binary trees called the left and right sub-trees of the original tree.
A left or right sub-tree can be empty.
Each element of a binary tree is called a node of the tree.
A Graph is just like a tree, it is a collection of objects or entities that we call nodes or vertices connected to each other
through a set of edges.
In a tree, all nodes must be reachable from the root node and there must be exactly one possible path from root to a
node
In graph, there are no rules dictating the connection among the nodes.
A graph contains a set of nodes and a set of edges can be connecting nodes in any possible way.
Graph data structure is often referred to as Graph Theory.
*when you say, an ordered pair, is just a pair of mathematical objects in which the order of objects in the pair matters.
So, this is how we write and represent an ordered pair of objects, separated by a comma and put within parenthesis
(V, E). Meaning the order here matters. V should be the first object in the pair and E is the second object in the pair.
If I have an ordered pair of (a,b), it is not equal to (b,a), unless a and b are equal.
(a, b) ≠ (b, a)
Unordered pair – simply a set of two elements in which the order is not important.
{a, b} = {b, a}
Representation of a Graph
1. Edge List representation
2. Adjacency Matric representation
3. Adjacency List representation
DIJKSTRA’S ALGORITHM
Dijkstra's algorithm to find the shortest path between nodes in a graph. It picks the unvisited vertex with the lowest
distance, calculates the distance through it to each unvisited neighbor, and updates the neighbor's distance if smaller.
Mark visited when done with neighbors.
PRIMS ALGORITHM
Prim's algorithm is to find a minimum spanning tree for a weighted undirected graph. This means it finds a subset of the
edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized.