yeasir007

Tuesday, January 30, 2018

Implement Graph without STL in C++/Java

A graph is a data structure which is a pictorial representation of a set of objects where pairs of objects are connected by links.

[problem]
Print the vertices that are adjacent to the given vertex in query.

[Input]
The first line contains the number of vertices V, the number of edges E and the number of queries Q.
From the second line on, the edge information (number of linked vertices pairs) will be given in ascending order.
On the next line, queries given as vertex numbers (asking for the adjacent vertices) will be given.
Also, edges do not overlap.
Print on each line the adjacent vertices of the given vertex. (2<=V<=100, 1<=E<=1000)

[Output] For each given vertex, print adjacent vertexes.


 INPUT                                                
=============                                                
2 // number of Test cases
6 7 3 // number of vertices, edges, queries
0 1 // Edge Information 0 - 1
0 2 0 3 1 2 1 4 3 4 4 5
0 // Query: Vertex Number
2
4
9 10 3
0 1
0 2
0 6
1 3
1 4
1 7
2 4
4 5
6 7
7 8
0
1
7

OUTPUT
===========================
#1
1 2 3 // adjacent vertices of vertex 0
0 1 // adjacent vertices of vertex 2
1 3 5 // adjacent vertices of vertex 4
#2
1 2 6
0 3 4 7
1 6 8


C++ Source :

    #include<iostream>
    #include <stdio.h>
    #include <malloc.h>

    using namespace std;

    typedef struct adjlistNode
    {
        int vertex;
        adjlistNode *next;
    } AdjlistNode;

    typedef struct
    {
        int num_members;
        AdjlistNode *head;
        AdjlistNode *tail;
    } AdjList;

    typedef struct
    {
        int num_vertices;
        AdjList * adjListArr;
    } Graph;

    AdjlistNode * createNode(int v);
    Graph * createGraph(int n);
    void destroyGraph(Graph * graph);
    void addEdge(Graph *graph, int src, int dest);
    void displayGraph(Graph * graph, int i);



    int main(int argc, char* argv[])
    {
        int T, V, E, Q, sv, ev;
        freopen("CreateGraph.txt", "r", stdin);
        cin >> T;

        for (int test_case = 1; test_case <= T; test_case++)
        {
            cin >> V >> E >> Q;
            Graph * graph = createGraph(V);

            for (int i = 0; i < E; i++)
            {
                cin >> sv >> ev;
                addEdge(graph, sv, ev);
            }

            cout << "#" << test_case << endl;

            for (int i = 0; i < Q; i++)
            {
                cin >> sv;
                displayGraph(graph, sv);
            }
        }

        return 0;
    }

    AdjlistNode * createNode(int v)
    {
        AdjlistNode * newNode = (AdjlistNode *)malloc(sizeof(AdjlistNode));

        newNode->vertex = v;
        newNode->next = NULL;

        return newNode;
    }

    Graph * createGraph(int n)
    {

        Graph * graph = (Graph *)malloc(sizeof(Graph));
        graph->num_vertices = n;

        graph->adjListArr = (AdjList *)malloc(n * sizeof(AdjList));

        for (int i = 0; i < n; i++)
        {
            graph->adjListArr[i].head = graph->adjListArr[i].tail = NULL;
            graph->adjListArr[i].num_members = 0;
        }

        return graph;
    }

    void destroyGraph(Graph * graph)
    {
        if (graph)
        {
            if (graph->adjListArr)
            {
                for (int v = 0; v < graph->num_vertices; v++)
                {
                    AdjlistNode * adjListPtr = graph->adjListArr[v].head;
                    while (adjListPtr)
                    {
                        AdjlistNode * tmp = adjListPtr;
                        adjListPtr = adjListPtr->next;
                        free(tmp);
                    }
                }
                free(graph->adjListArr);
            }
            free(graph);
        }
    }

    void addEdge(Graph *graph, int src, int dest)
    {
        AdjlistNode * newNode = createNode(dest);
        if (graph->adjListArr[src].tail != NULL)
        {
            graph->adjListArr[src].tail->next = newNode;
            graph->adjListArr[src].tail = newNode;
        }
        else
        {
            graph->adjListArr[src].head = graph->adjListArr[src].tail = newNode;
        }
        graph->adjListArr[src].num_members++;

        newNode = createNode(src);
        if (graph->adjListArr[dest].tail != NULL)
        {
            graph->adjListArr[dest].tail->next = newNode;
            graph->adjListArr[dest].tail = newNode;
        }
        else
        {
            graph->adjListArr[dest].head = graph->adjListArr[dest].tail = newNode;
        }
        graph->adjListArr[dest].num_members++;
    }

    void displayGraph(Graph * graph, int i)
    {

        AdjlistNode * adjListPtr = graph->adjListArr[i].head;
        while (adjListPtr)
        {
            cout << adjListPtr->vertex;
            adjListPtr = adjListPtr->next;
        }
        cout << endl;
    }


Java Source :

    package com.datastructure;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.util.Scanner;

    public class GraphView {

        static private final String INPUT = System.getProperty("user.dir") + "\\src\\data\\graphInput.txt";
        static Scanner sc;

        public static void main(String[] args) {
            FileInputStream instream = null;
            try {
                instream = new FileInputStream(INPUT);
                System.setIn(instream);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            sc = new Scanner(System.in);

            int T = sc.nextInt();

            for (int test_case = 1; test_case <= T; test_case++)
            {
                int V = sc.nextInt();
                int E = sc.nextInt();
                int Q = sc.nextInt();
                Graph graph = new Graph(V);
                for (int i = 0; i < E; i++)
                {
                    int sv = sc.nextInt();
                    int ev = sc.nextInt();
                    graph.addEdge(sv, ev);
                }
                System.out.printf("#%d\n", test_case);
                for (int i = 0; i < Q; i++)
                {
                    int sv = sc.nextInt();
                    graph.display(sv);
                }
            }
            sc.close();

        }

    }

    class Graph
    {

        class AdjlistNode
        {
            int vertex;
            AdjlistNode next;

            public AdjlistNode(int v)
            {
                vertex = v;
                next = null;
            }
        }

        class AdjList
        {
            int num_members;
            AdjlistNode head;
            AdjlistNode tail;

            public AdjList()
            {
                num_members = 0;
                head = tail = null;
            }
        }

        int num_vertices;
        AdjList []adjListArr;

        public Graph(int n)
        {
            num_vertices = n;
            adjListArr = new AdjList[n];
            for (int i = 0; i < n; i++)
            {
                adjListArr[i] = new AdjList();
            }
        }

        void addEdge(int src, int dest)
        {
            AdjlistNode newNode = new AdjlistNode(dest);
            if (adjListArr[src].tail != null)
            {
                adjListArr[src].tail.next = newNode;
                adjListArr[src].tail = newNode;
            }
            else 
            {
                adjListArr[src].head = adjListArr[src].tail = newNode;
            }
            adjListArr[src].num_members++;

            newNode = new AdjlistNode(src);
            if (adjListArr[dest].tail != null)
            {
                adjListArr[dest].tail.next = newNode;
                adjListArr[dest].tail = newNode;
            }
            else 
            {
                adjListArr[dest].head = adjListArr[dest].tail = newNode;
            }
            adjListArr[dest].num_members++;
        }

        void display(int i)
        {
            AdjlistNode adjList = adjListArr[i].head;
            while (adjList != null)
            {
                System.out.printf("%d ", adjList.vertex);
                adjList = adjList.next;
            }
            System.out.printf("\n");
        }
    }



No comments:

Post a Comment

Thanks for your comments