Found 345 Articles for Data Structure Algorithms

Binomial Distribution in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 12:41:00

283 Views

The Binomial Distribution is a discrete probability distribution Pp(n | N) of obtaining n successes out of N Bernoulli trails (having two possible outcomes labeled by x = 0 and x = 1. The x = 1 is success, and x = 0 is failure. Success occurs with probability p, and failure occurs with probability q as q = 1 – p.) So the binomial distribution can be written as$$P_{p}\lgroup n\:\arrowvert\ N\rgroup=\left(\begin{array}{c}N\ n\end{array}\right) p^{n}\lgroup1-p\rgroup^{N-n}$$Example Live Demo#include #include using namespace std; int main(){    const int nrolls = 10000; // number of rolls    const int nstars = 100; // ... Read More

Bernoulli Distribution in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 12:33:29

266 Views

The Bernoulli Distribution is a discrete distribution having two possible outcomes labeled by x = 0 and x = 1. The x = 1 is success, and x = 0 is failure. Success occurs with probability p, and failure occurs with probability q as q = 1 – p. So$$P\lgroup x\rgroup=\begin{cases}1-p\:for & x = 0\p\:for & x = 0\end{cases}$$This can also be written as −$$P\lgroup x\rgroup=p^{n}\lgroup1-p\rgroup^{1-n}$$Example Live Demo#include #include using namespace std; int main(){    const int nrolls=10000;    default_random_engine generator;    bernoulli_distribution distribution(0.7);    int count=0; // count number of trues    for (int i=0; i

Minimum Spanning Tree in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 12:29:59

16K+ Views

A spanning tree is a subset of an undirected Graph that has all the vertices connected by minimum number of edges.If all the vertices are connected in a graph, then there exists at least one spanning tree. In a graph, there may exist more than one spanning tree.Minimum Spanning TreeA Minimum Spanning Tree (MST) is a subset of edges of a connected weighted undirected graph that connects all the vertices together with the minimum possible total edge weight. To derive an MST, Prim’s algorithm or Kruskal’s algorithm can be used. Hence, we will discuss Prim’s algorithm in this chapter.As we ... Read More

Applications of DFS and BFS in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 12:25:37

17K+ Views

Here we will see what are the different applications of DFS and BFS algorithms of a graph?The DFS or Depth First Search is used in different places. Some common uses are −If we perform DFS on unweighted graph, then it will create minimum spanning tree for all pair shortest path treeWe can detect cycles in a graph using DFS. If we get one back-edge during BFS, then there must be one cycle.Using DFS we can find path between two given vertices u and v.We can perform topological sorting is used to scheduling jobs from given dependencies among jobs. Topological sorting ... Read More

Graphs and its traversal algorithms

Arnab Chakraborty
Updated on 08-Sep-2023 23:02:20

42K+ Views

In this section we will see what is a graph data structure, and the traversal algorithms of it.The graph is one non-linear data structure. That is consists of some nodes and their connected edges. The edges may be director or undirected. This graph can be represented as G(V, E). The following graph can be represented as G({A, B, C, D, E}, {(A, B), (B, D), (D, E), (B, C), (C, A)})The graph has two types of traversal algorithms. These are called the Breadth First Search and Depth First Search.Breadth First Search (BFS)The Breadth First Search (BFS) traversal is an algorithm, ... Read More

Binary Search Trees in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 12:13:24

528 Views

The binary search trees are binary tree which has some properties. These properties are like below −Every Binary Search Tree is a binary treeEvery left child will hold lesser value than rootEvery right child will hold greater value than rootIdeal binary search tree will not hold same value twice.Suppose we have one tree like this −This tree is one binary search tree. It follows all of the mentioned properties. If we traverse elements into inorder traversal mode, we can get 5, 8, 10, 15, 16, 20, 23. Let us see one code to understand how we can implement this in ... Read More

Preorder Tree Traversal in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 12:07:28

389 Views

In this section we will see the pre-order traversal technique (recursive) for binary search tree.Suppose we have one tree like this −The traversal sequence will be like: 10, 5, 8, 16, 15, 20, 23AlgorithmpreorderTraverse(root): Begin    if root is not empty, then       print the value of root       preorderTraversal(left of root)       preorderTraversal(right of root)    end if EndExample Live Demo#include using namespace std; class node{    public:       int h_left, h_right, bf, value;       node *left, *right; }; class tree{    private:       node *get_node(int key);   ... Read More

Postorder Tree Traversal in Data Structures

Arnab Chakraborty
Updated on 21-Jan-2020 12:17:37

349 Views

In this section we will see the post-order traversal technique (recursive) for binary search tree.Suppose we have one tree like this −The traversal sequence will be like: 8, 5, 15, 23, 20, 16, 10AlgorithmpostorderTraverse(root): Begin    if root is not empty, then       postorderTraversal(left of root)       postorderTraversal(right of root)       print the value of root    end if EndExample Live Demo#include using namespace std; class node{    public:       int h_left, h_right, bf, value;       node *left, *right; }; class tree{    private:       node *get_node(int key);   ... Read More

Level Order Tree Traversal in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 11:34:58

335 Views

In this section we will see the level-order traversal technique for binary search tree.Suppose we have one tree like this −The traversal sequence will be like: 10, 5, 16, 8, 15, 20, 23AlgorithmlevelOrderTraverse(root): Begin    define queue que to store nodes    insert root into the que.    while que is not empty, do       item := item present at front position of queue       print the value of item       if left of the item is not null, then          insert left of item into que       end ... Read More

Binary Tree Traversals in Data Structures

Arnab Chakraborty
Updated on 27-Aug-2019 11:23:47

816 Views

In this section we will see different traversal algorithms to traverse keys present in binary search tree. These traversals are Inorder traversal, Preorder traversal, Postorder traversal and the level order traversal.Suppose we have one tree like this −The Inorder traversal sequence will be like − 5 8 10 15 16 20 23The Preorder traversal sequence will be like − 10 5 8 16 15 20 23The Postorder traversal sequence will be like − 8 5 15 23 20 16 10The Level-order traversal sequence will be like − 10, 5, 16, 8, 15, 20, 23AlgorithminorderTraverse(root): Begin    if root is not ... Read More

Advertisements