Tree and Graph Tree and graph can be classified under the non linear data structure. Here I have shared few algorithms of tree and graph. Graph algorithms Algorithm for DFS(Depth First Search) graph traversal: Step 1:Define a Stack of size total number of vertices in the graph. Step 2:Select any vertex as starting point for traversal.Visit that vertex and push it on to the Stack. Step 3:Visit any one of the adjacent vertex of the vertex which is at top of the stack which is not visited and push it on to the stack. Step 5:When there is no new vertex to be visit then use back tracking and pop one vertex from the stack. Step 6:Repeat steps 3, 4 and 5 until stack becomes Empty. Step 7:When stack becomes Empty, then produce final spanning tree by removing unused edges from the graph. Algorithm for BFS(Breadth First Search) graph traversal: Step 1:Define a Queue of size total number of vertices in the graph. Step 2:Select any vertex as starting point for traversal.Visit ...
Linear Data Structure Algorithm: Part II Linked List is another variation of Linear Data Structure. Though it is categorized in Linear Data Structure, it is not contiguous in nature. Rather it stores nodes in different locations at memory and links the nodes to each other forming a list, Therefore derives the name Linked List. Single Linked List Algorithm to create a node in Single Linked List: Allocate memory for ptr (pointer to new node) If ptr = NULL print: Overflow: Memory not allocated End If Read Data to be inserted in node Set nptr->info = data Set nptr->next = NULL return nptr End Algorithm to Count Node in Linked List: Set count to 0 Set temp to First While temp != NULL Set count = count + 1 Set temp = temp->next End While return count End Algorithm to Reverse a given Linked List: reverse(First) Set temp = First ...