Found 34484 Articles for Programming

C++ Program to Generate a Graph for a Given Fixed Degree Sequence

Chandu yadav
Updated on 30-Jul-2019 22:30:25

196 Views

This is a C++ program represents a undirected graph for the given degree sequence. The time complexity of this algorithm is O(v*v) and it does not include self-edge and multiple edges.AlgorithmBegin    To create the graph,    Create the first loop to connect each vertex ‘i’.    Create second nested loop to connect the vertex ‘i’ to every valid vertex ‘j’, next to it.    If the degree of vertex ‘i’ and ‘j’ are more than zero, then connect them.    Print the adjacency matrix using PrintMatrix(). EndExample Code#include #include using namespace std; void PrintMatrix(int matrix[][20], int n) {   ... Read More

C++ Program to Generate a Random Directed Acyclic Graph DAC for a Given Number of Edges

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

438 Views

In this program we generate a random directed acyclic graph for the given edges ‘e’. The time complexity of this program is O(e*v*e).AlgorithmBegin    function GenerateRandomGraphs(), has ‘e’ as the number edges in the argument list.    generate a connection between two random numbers, for sample a small case, limit the number of vertex to 20.    Check for the cycle in the graph Using CheckAcyclic(),    if it returns false discard this edge.       Else maintain the edge in the graph.    Print all the directed connection of each vertex.    If the in+out degree of each ... Read More

C++ Program to Find the peak element of an array using Binary Search approach

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

775 Views

In this C++ program, we find out one of the peaks in the array can be found Using binary search approach. This algorithm returns the first peak found as a result with time complexity of the algorithm is O(log(n)).AlgorithmBegin    PeakElement() function has ‘arr’ the array of data, start and end index in the argument list.    Assign the mid of subpart of the array.    If mid is at the boundary index and value at mid is higher than its neighbor then return mid as peak.    If the value at mid is greater than both of its neighbors ... Read More

C++ Program to Find the Minimum element of an Array using Binary Search approach

George John
Updated on 30-Jul-2019 22:30:25

178 Views

This is a C++ Program to find the minimum element of an array using Linear Search approach. The time complexity of this program is O(log(n)).AlgorithmBegin Construct binary search tree for the given unsorted data array. To find out the minimum element move the pointer to the leftmost child node. Print this value as minimum value among the given data. EndExample Code#include using namespace std; struct node {    int d;    node *left;    node *right; }; node* CreateNode(int d) {    node *newnode = new node;    newnode->d = d; ... Read More

C++ Program to Find Maximum Element in an Array using Binary Search

Chandu yadav
Updated on 30-Jul-2019 22:30:25

383 Views

This is a C++ Program to find the maximum element of an array using Binary Search Tree. The time complexity of this program is O(log(n)).AlgorithmBegin Construct the Binary Search Tree using the given data elements. Next traverse the root pointer to the rightmost child node available. Print the data part of the node as the maximum data element of the given data set. Print the Depth of the maximum data. EndExample Code#include using namespace std; struct node {    int d;    node *left;    node *right; }; ... Read More

C++ Program to Find Minimum Element in an Array using Linear Search

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

758 Views

This is a C++ Program to find the minimum element of an array using Linear Search approach. The time complexity of this program is O(n).AlgorithmBegin Assign the data element to an array. Assign the value at ‘0’ index to minimum variable. Compare minimum with other data element sequentially. Swap values if minimum value is more then the value at that particular index of the array. print the minimum value. EndExample Code#include using namespace std; int main() { int n, i, minimum, a[10] = {1, 6, 7, 10, 12, 14, 12, 16, 20, 26}; char ch; minimum = a[0]; cout

C++ Program to Perform Uniform Binary Search

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

215 Views

In uniform binary search here we implement binary search using a lookup table. It is an improvement in binary search since table lookup is faster than a shift and addition. The time complexity of this approach is O(log(n)).AlgorithmBegin    Assign the data to the array in a sorted manner.    Calculate the maximum length of lookup array and declare a new array ‘del’.    Assign the values to the lookup array as n/2, n/4 and so on till ‘0’, where n is the length of the data    array.    Call UniBinarySearch() function.    Assign mid to the value at ... Read More

C++ Program to Search Sorted Sequence Using Divide and Conquer with the Aid of Fibonacci Numbers

George John
Updated on 30-Jul-2019 22:30:25

388 Views

In this C++ program we implement a Divide and Conquer approach using Fibonacci numbers. Using Fibonacci numbers, we calculate mid of data array to search the data item. The time complexity of this approach is O(log(n)).AlgorithmBegin    Assign the data to the array in a sorted manner.    Take input of the element to be searched.    Call FibonacciSearch() function.    Calculate the mid value using ‘start+fib[index-2]’ expression.    If the chosen item is equal to the value at mid index, print result and return to main.    If it is lesser than the value at mid index, proceed with ... Read More

C++ Program to Find kth Largest Element in a Sequence

Chandu yadav
Updated on 30-Jul-2019 22:30:25

210 Views

In this program, we need to extract the Kth largest element from a sequence. the time complexity of this technique can be improved by approaching the problem using max-heap. The time complexity of this program is O(n + k*log(n)).AlgorithmBegin    Send the max of the heap to the end of the sequence.    Heapify the remaining sequence.    Repeat the process for ‘k’ times.    Print the final state of the array.    Print the max from the heap extracted from kth iteration as a result. End.Example Code#include using namespace std; void MaxHeapify(int a[], int i, int n) { ... Read More

C++ Program to Search for an Element in a Binary Search Tree

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

2K+ Views

In this program we need to. Implement binary search to find the existence of a search sequence in a binary search tree. The worst case time complexity of Binary search is O(n) but for the average case O(log(n)).AlgorithmBegin    Construct binary search tree for the given unsorted data array by inserting data into tree one by one.    Take the input of data to be searched in the BST.    Now starting from the root node, compare the data with data part of the node.    if data < temp->d, move the temp pointer to the left child.    if ... Read More

Advertisements