Friday, 19 May 2017

Write a C Program for Depth First Binary Tree Search Recursion in Linux.

 C Program for Depth First Binary Tree Search Recursion in Linux.

Source Code:

#include <stdio.h>

#include <stdlib.h>



struct node

{

    int a;

    struct node *left;

    struct node *right;

};



void generate(struct node **, int);

void DFS(struct node *);

void delete(struct node **);



int main()

{

    struct node *head = NULL;

    int choice = 0, num, flag = 0, key;



    do

    {

        printf("\nEnter your choice:\n1. Insert\n2. Perform DFS Traversal\n3. Exit\nChoice: ");

        scanf("%d", &choice);

        switch(choice)

        {

        case 1: 

            printf("Enter element to insert: ");

            scanf("%d", &num);

            generate(&head, num);

            break;

        case 2: 

            DFS(head);

            break;

        case 3: 

            delete(&head);

            printf("Memory Cleared\nPROGRAM TERMINATED\n");

            break;

        default: 

            printf("Not a valid input, try again\n");

        }

    } while (choice != 3);

    return 0;

}



void generate(struct node **head, int num)

{

    struct node *temp = *head, *prev = *head;



    if (*head == NULL)

    {

        *head = (struct node *)malloc(sizeof(struct node));

        (*head)->a = num;

        (*head)->left = (*head)->right = NULL;

    }

    else

    {

        while (temp != NULL)

        {

            if (num > temp->a)

            {

                prev = temp;

                temp = temp->right;

            }

            else

            {

                prev = temp;

                temp = temp->left;

            }

        }

        temp = (struct node *)malloc(sizeof(struct node));

        temp->a = num;

        if (num >= prev->a)

        {

            prev->right = temp;

        }

        else

        {

            prev->left = temp;

        }

    }

}



void DFS(struct node *head)

{

    if (head)

    {

        if (head->left)

        {

            DFS(head->left);

        }

        if (head->right)

        {

            DFS(head->right);

        }

        printf("%d  ", head->a);

    }

}



void delete(struct node **head)

{

    if (*head != NULL)

    {

        if ((*head)->left)

        {

            delete(&(*head)->left);

        }

        if ((*head)->right)

        {

            delete(&(*head)->right);

        }

        free(*head);

    }

}



OUTPUT 

$ cc pgm34.c
$ a.out

Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 5

Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 3

Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 4

Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 2

Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 7

Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 8

Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 6

Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 2
2  4  3  6  8  7  5  
Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 3
Memory Cleared
PROGRAM TERMINATED

Write a C Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements From 1 to N.

C Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements From 1 to N.

It is successfully compiled and run on a Linux system.

Source Code:

   
 #include <stdio.h>

    void print(const int *v, const int size)

    {

        int i;

        if (v != 0) {

        for ( i = 0; i < size; i++) {

            printf("%4d", v[i] );

        }

        printf("\n");

      }

    }

    void alexanderbogomolyn(int *Value, int N, int k)

    {

        static level = -1;

        int i;

        level = level+1; Value[k] = level;

        if (level == N)

            print(Value, N);

        else

            for (i = 0; i < N; i++)

        if (Value[i] == 0)

            alexanderbogomolyn(Value, N, i);

        level = level-1;

        Value[k] = 0;

    }

    int main()

    {

        int N = 4;

        int i;

        int Value[N];

        for (i = 0; i < N; i++) {

          Value[i] = 0;

        }

        printf("\nPermutation using Alexander Bogomolyn's algorithm: ");

        alexanderbogomolyn(Value, N, 0);

        return 0;

    }

OUTPUT


$ gcc permute.c -o permute
$ ./permute
Permutation using Alexander Bogomolyns algorithm:
   1   2   3   4
   1   2   4   3
   1   3   2   4
   1   4   2   3
   1   3   4   2
   1   4   3   2
   2   1   3   4
   2   1   4   3
   3   1   2   4
   4   1   2   3
   3   1   4   2
   4   1   3   2
   2   3   1   4
   2   4   1   3
   3   2   1   4
   4   2   1   3
   3   4   1   2
   4   3   1   2
   2   3   4   1
   2   4   3   1
   3   2   4   1
   4   2   3   1
   3   4   2   1
   4   3   2   1




 

 

Write a C Program to Generate All Possible Combinations of a Given List of Numbers in Linux.

C Program to Generate All Possible Combinations of a Given List of Numbers.

It's successfully compiled and run on a Linux system. 

Source Code:

    #include<stdio.h>

   
 #include<string.h>

    #define N 10


    void print(int *num, int n)

    {

        int i;

        for ( i = 0 ; i < n ; i++)

            printf("%d ", num[i]);

        printf("\n");

    }

    int main()

    {

        int num[N];

        int *ptr;

        int temp;

        int i, n, j;

        printf("\nHow many number you want to enter: ");

            scanf("%d", &n);

        printf("\nEnter a list of numbers to see all combinations:\n");

        for (i = 0 ; i < n; i++)

            scanf("%d", &num[i]);

        for (j = 1; j <= n; j++) {

            for (i = 0; i < n-1; i++) {

                temp = num[i];

                num[i] = num[i+1];

                num[i+1] = temp;

                print(num, n);

        }

        }

        return 0;

    }


OUTPUT

$ gcc combination.c -o combination
$ ./combination
How many number you want to enter: 4
Enter a list of numbers to see all combinations: 1 2 3 4
2 1 3 4 
2 3 1 4 
2 3 4 1 
3 2 4 1 
3 4 2 1 
3 4 1 2 
4 3 1 2 
4 1 3 2 
4 1 2 3 
1 4 2 3 
1 2 4 3 
1 2 3 4
 
 
 

Write a C Program to Permute All Letters of an Input String in Linux System.

A C Program to Permute All Letters of an Input String in Linux System. 


This algorithm finds all permutations of the letters of a given string. It is a recursive algorithm using concept of backtracking.

The source code for the C program to permute all letters of an input string. This program is successfully compiled and run on a Linux system.


    #include <stdio.h>

    #include <string.h>

    void swap (char *x, char *y)

    {

        char temp;

        temp = *x;

        *x = *y;

        *y = temp;

    }

    void permute(char *a, int i, int n) 

    {

       int j; 

       if (i == n)

         printf("%s\n", a);

       else

       {

            for (j = i; j <= n; j++)

           {

              swap((a + i), (a + j));

              permute(a, i + 1, n);

              swap((a + i), (a + j)); //backtrack

           }

       }

    } 

int main()

    {

       char str[21];

       int len;

       printf("\nEnter a string: ");

       scanf("%s", str);

       len = strlen(str);

       permute(str, 0, len - 1);

       return 0;

    }


OUTPUT

$ gcc permute.c -o permute
$ ./permute

Enter a string: SAN

All possible permutations are:
    SAN
    SNA
    ASN
    ANS
    NAS
    NSA



Write a C++ Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected.

A C++ Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected.

A C++ Program to find minimum number of edges to cut to make the graph disconnected. An edge in an un-directed connected graph is a bridge if removing it disconnects the graph. For a disconnected un-directed graph, definition is similar, a bridge is an edge removing which increases number of connected components.

A Source Code for the C++ Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected. It's also successfully compiled and run on a Linux system.


#include<iostream>

#include <list>

#define NIL -1

using namespace std;



// A class that represents an undirected graph

class Graph

{

        int V; // No. of vertices

        list<int> *adj; // A dynamic array of adjacency lists

        void bridgeUtil(int v, bool visited[], int disc[], int low[],

                int parent[]);

    public:

        Graph(int V); // Constructor

        void addEdge(int v, int w); // function to add an edge to graph

        void bridge(); // prints all bridges

};



Graph::Graph(int V)

{

    this->V = V;

    adj = new list<int> [V];

}



void Graph::addEdge(int v, int w)

{

    adj[v].push_back(w);

    adj[w].push_back(v); // Note: the graph is undirected

}



void Graph::bridgeUtil(int u, bool visited[], int disc[], int low[],

        int parent[])

{

    // A static variable is used for simplicity, we can avoid use of static

    // variable by passing a pointer.

    static int time = 0;



    // Mark the current node as visited

    visited[u] = true;



    // Initialize discovery time and low value

    disc[u] = low[u] = ++time;



    // Go through all vertices aadjacent to this

    list<int>::iterator i;

    for (i = adj[u].begin(); i != adj[u].end(); ++i)

    {

        int v = *i; // v is current adjacent of u



        // If v is not visited yet, then recur for it

        if (!visited[v])

        {

            parent[v] = u;

            bridgeUtil(v, visited, disc, low, parent);



            // Check if the subtree rooted with v has a connection to

            // one of the ancestors of u

            low[u] = min(low[u], low[v]);



            // If the lowest vertex reachable from subtree under v is

            // below u in DFS tree, then u-v is a bridge

            if (low[v] > disc[u])

                cout << u << " " << v << endl;

        }



        // Update low value of u for parent function calls.

        else if (v != parent[u])

            low[u] = min(low[u], disc[v]);

    }

}



// DFS based function to find all bridges. It uses recursive function bridgeUtil()

void Graph::bridge()

{

    // Mark all the vertices as not visited

    bool *visited = new bool[V];

    int *disc = new int[V];

    int *low = new int[V];

    int *parent = new int[V];



    // Initialize parent and visited arrays

    for (int i = 0; i < V; i++)

    {

        parent[i] = NIL;

        visited[i] = false;

    }



    // Call the recursive helper function to find Bridges

    // in DFS tree rooted with vertex 'i'

    for (int i = 0; i < V; i++)

        if (visited[i] == false)

            bridgeUtil(i, visited, disc, low, parent);

}



// Driver program to test above function

int main()

{

    // Create graphs given in above diagrams

    cout << "\nBridges in first graph \n";

    Graph g1(5);

    g1.addEdge(1, 0);

    g1.addEdge(0, 2);

    g1.addEdge(2, 1);

    g1.addEdge(0, 3);

    g1.addEdge(3, 4);

    g1.bridge();



    cout << "\nBridges in second graph \n";

    Graph g2(4);

    g2.addEdge(0, 1);

    g2.addEdge(1, 2);

    g2.addEdge(2, 3);

    g2.bridge();



    cout << "\nBridges in third graph \n";

    Graph g3(7);

    g3.addEdge(0, 1);

    g3.addEdge(1, 2);

    g3.addEdge(2, 0);

    g3.addEdge(1, 3);

    g3.addEdge(1, 4);

    g3.addEdge(1, 6);

    g3.addEdge(3, 5);

    g3.addEdge(4, 5);

    g3.bridge();

    return 0;

} 



OUTPUT 

$ g++ Bridges.cpp

$ a.out
Bridges in first graph

3 4



Write a Program to Find the Maximum Size Clique in a Graph in C++.

A C++ Program to Find the Maximum Size Clique in a Graph.

 
 It's a C++ Program to find the cliques of size k in  a graph. An un-directed graph which is formed by a finite set of vertices and a set of un-ordered pairs of vertices, which are called edges. In algorithm analysis, the number of vertices in the graph is denoted by n and the number of edges is denoted by m. A clique in a graph G is a complete sub-graph of G; that is, it is a subset S of the vertices such that every two vertices in S are connected by an edge in G. A maximal clique is a clique to which no more vertices can be added; a maximum clique is a clique that includes the largest possible number of vertices, and the clique number ?(G) is the number of vertices in a maximum clique of G.


Here is the source code for the C++ Program to Find the Maximum Size Clique in a Graph which is
successfully compiled and run on a Linux system.

    #include <iostream>

    #include <fstream>

    #include <string>

    #include <vector>

    using namespace std;

     

    bool removable(vector<int> neighbor, vector<int> cover);

    int max_removable(vector<vector<int> > neighbors, vector<int> cover);

    vector<int> procedure_1(vector<vector<int> > neighbors, vector<int> cover);

    vector<int> procedure_2(vector<vector<int> > neighbors, vector<int> cover,

            int k);

    int cover_size(vector<int> cover);

    ifstream infile("graph.txt");

    ofstream outfile("cliques.txt");

     

    int main()

    {

        
        cout << "Clique Algorithm." << endl;

        int n, i, j, k, K, p, q, r, s, min, edge, counter = 0;

        infile >> n;

        vector<vector<int> > graph;

        for (i = 0; i < n; i++)

        {

            vector<int> row;

            for (j = 0; j < n; j++)

            {

                infile >> edge;

                if (edge == 0)

                    row.push_back(1);

                else

                    row.push_back(0);

            }

            graph.push_back(row);

        }

        // To Find Neighbors

        vector<vector<int> > neighbors;

        for (i = 0; i < graph.size(); i++)

        {

            vector<int> neighbor;

            for (j = 0; j < graph[i].size(); j++)

                if (graph[i][j] == 1)

                    neighbor.push_back(j);

            neighbors.push_back(neighbor);

        }

        cout << "Graph has n = " << n << " vertices." << endl;

        // To Read maximum size of Clique wanted

        cout << "Find a Clique of size at least k = ";

        cin >> K;

        k = n - K;

        //Find Cliques

        bool found = false;

        cout << "Finding Cliques..." << endl;

        min = n + 1;

        vector<vector<int> > covers;

        vector<int> allcover;

        for (i = 0; i < graph.size(); i++)

            allcover.push_back(1);

        for (i = 0; i < allcover.size(); i++)

        {

            if (found)

                break;

            counter++;

            cout << counter << ". ";

            outfile << counter << ". ";

            vector<int> cover = allcover;

            cover[i] = 0;

            cover = procedure_1(neighbors, cover);

            s = cover_size(cover);

            if (s < min)

                min = s;

            if (s <= k)

            {

                outfile << "Clique (" << n - s << "): ";

                for (j = 0; j < cover.size(); j++)

                    if (cover[j] == 0)

                        outfile << j + 1 << " ";

                outfile << endl;

                cout << "Clique Size: " << n - s << endl;

                covers.push_back(cover);

                found = true;

                break;

            }

            for (j = 0; j < n - k; j++)

                cover = procedure_2(neighbors, cover, j);

            s = cover_size(cover);

            if (s < min)

                min = s;

            outfile << "Clique (" << n - s << "): ";

            for (j = 0; j < cover.size(); j++)

                if (cover[j] == 0)

                    outfile << j + 1 << " ";

            outfile << endl;

            cout << "Clique Size: " << n - s << endl;

            covers.push_back(cover);

            if (s <= k)

            {

                found = true;

                break;

            }

        }

        //Pairwise Intersections

        for (p = 0; p < covers.size(); p++)

        {

            if (found)

                break;

            for (q = p + 1; q < covers.size(); q++)

            {

                if (found)

                    break;

                counter++;

                cout << counter << ". ";

                outfile << counter << ". ";

                vector<int> cover = allcover;

                for (r = 0; r < cover.size(); r++)

                    if (covers[p][r] == 0 && covers[q][r] == 0)

                        cover[r] = 0;

                cover = procedure_1(neighbors, cover);

                s = cover_size(cover);

                if (s < min)

                    min = s;

                if (s <= k)

                {

                    outfile << "Clique (" << n - s << "): ";

                    for (j = 0; j < cover.size(); j++)

                        if (cover[j] == 0)

                            outfile << j + 1 << " ";

                    outfile << endl;

                    cout << "Clique Size: " << n - s << endl;

                    found = true;

                    break;

                }

                for (j = 0; j < k; j++)

                    cover = procedure_2(neighbors, cover, j);

                s = cover_size(cover);

                if (s < min)

                    min = s;

                outfile << "Clique (" << n - s << "): ";

                for (j = 0; j < cover.size(); j++)

                    if (cover[j] == 0)

                        outfile << j + 1 << " ";

                outfile << endl;

                cout << "Clique Size: " << n - s << endl;

                if (s <= k)

                {

                    found = true;

                    break;

                }

            }

        }

        if (found)

            cout << "Found Clique of size at least " << K << "." << endl;

        else

            cout << "Could not find Clique of size at least " << K << "." << endl

                    << "Maximum Clique size found is " << n - min << "." << endl;

        cout << "See cliques.txt for results." << endl;

        return 0;

    }

     

    bool removable(vector<int> neighbor, vector<int> cover)

    {

        bool check = true;

        for (int i = 0; i < neighbor.size(); i++)

            if (cover[neighbor[i]] == 0)

            {

                check = false;

                break;

            }

        return check;

    }

     

    int max_removable(vector<vector<int> > neighbors, vector<int> cover)

    {

        int r = -1, max = -1;

        for (int i = 0; i < cover.size(); i++)

        {

            if (cover[i] == 1 && removable(neighbors[i], cover) == true)

            {

                vector<int> temp_cover = cover;

                temp_cover[i] = 0;

                int sum = 0;

                for (int j = 0; j < temp_cover.size(); j++)

                    if (temp_cover[j] == 1 && removable(neighbors[j], temp_cover)

                            == true)

                        sum++;

                if (sum > max)

                {

                    max = sum;

                    r = i;

                }

            }

        }

        return r;

    }

     

    vector<int> procedure_1(vector<vector<int> > neighbors, vector<int> cover)

    {

        vector<int> temp_cover = cover;

        int r = 0;

        while (r != -1)

        {

            r = max_removable(neighbors, temp_cover);

            if (r != -1)

                temp_cover[r] = 0;

        }

        return temp_cover;

    }

     

    vector<int> procedure_2(vector<vector<int> > neighbors, vector<int> cover,

            int k)

    {

        int count = 0;

        vector<int> temp_cover = cover;

        int i = 0;

        for (int i = 0; i < temp_cover.size(); i++)

        {

            if (temp_cover[i] == 1)

            {

                int sum = 0, index;

                for (int j = 0; j < neighbors[i].size(); j++)

                    if (temp_cover[neighbors[i][j]] == 0)

                    {

                        index = j;

                        sum++;

                    }

                if (sum == 1 && cover[neighbors[i][index]] == 0)

                {

                    temp_cover[neighbors[i][index]] = 1;

                    temp_cover[i] = 0;

                    temp_cover = procedure_1(neighbors, temp_cover);

                    count++;

                }

                if (count > k)

                    break;

            }

        }

        return temp_cover;

    }

     

    int cover_size(vector<int> cover)

    {

        int count = 0;

        for (int i = 0; i < cover.size(); i++)

            if (cover[i] == 1)

                count++;

        return count;

    }





OUTPUT

 

 
$ g++ MaxClique.cpp
$ a.out
 
graph.txt:
4
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
 
cliques.txt:
Clique ( 4 ): 1 2 3 4
------------------
(program exited with code: 0)
Press return to continue

 

 





Thursday, 18 May 2017

How to Know IP Location with their city code,Region Code, Country Code,Latitude & Longitude also.

A Simple function for finding the IP location  with their city code,Region Code, Country Code, Latitude & Longitude.



<html>

<head>

  <title>Display IP Location with Full Information </title>

  <link href="<?php echo base_url(); ?>css/bootstrap.min.css" rel="stylesheet">

</head>

  

<body>

    

 <div class="fluid-container">

   <div class="row">

      <div class="col-md-12">&nbsp;</div>

         <div class="col-md-12">

          <div class="col-md-3"></div>

           <div class="col-md-6">

           <?php

           function getiplocationdetails()

          {

             $ip = '192.190.85.178';

             $result = '';

             $ip_datails = @json_decode

            (file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip)); 

             return $ip_datails;

              }





              echo "<>";

              print_r(getiplocationdetails());

              ?>

              </div>

              <div class="col-md-3"></div>

            </div>

         </div>

     </div>

  </body>

</html>