"how to make graphs in c++" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "how to make graphs in c++" answered properly. Developers are finding an appropriate answer about how to make graphs in c++ related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like how to make graphs in c++. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on how to make graphs in c++. 

how to make graphs in c++

By Silly SharkSilly Shark on Mar 30, 2021
#include <iostream>
using namespace std;
// stores adjacency list items
struct adjNode {
    int val, cost;
    adjNode* next;
};
// structure to store edges
struct graphEdge {
    int start_ver, end_ver, weight;
};
class DiaGraph{
    // insert new nodes into adjacency list from given graph
    adjNode* getAdjListNode(int value, int weight, adjNode* head)   {
        adjNode* newNode = new adjNode;
        newNode->val = value;
        newNode->cost = weight;
         
        newNode->next = head;   // point new node to current head
        return newNode;
    }
    int N;  // number of nodes in the graph
public:
    adjNode **head;                //adjacency list as array of pointers
    // Constructor
    DiaGraph(graphEdge edges[], int n, int N)  {
        // allocate new node
        head = new adjNode*[N]();
        this->N = N;
        // initialize head pointer for all vertices
        for (int i = 0; i < N; ++i)
            head[i] = nullptr;
        // construct directed graph by adding edges to it
        for (unsigned i = 0; i < n; i++)  {
            int start_ver = edges[i].start_ver;
            int end_ver = edges[i].end_ver;
            int weight = edges[i].weight;
            // insert in the beginning
            adjNode* newNode = getAdjListNode(end_ver, weight, head[start_ver]);
             
                        // point head pointer to new node
            head[start_ver] = newNode;
             }
    }
      // Destructor
     ~DiaGraph() {
    for (int i = 0; i < N; i++)
        delete[] head[i];
        delete[] head;
     }
};
// print all adjacent vertices of given vertex
void display_AdjList(adjNode* ptr, int i)
{
    while (ptr != nullptr) {
        cout << "(" << i << ", " << ptr->val
            << ", " << ptr->cost << ") ";
        ptr = ptr->next;
    }
    cout << endl;
}
// graph implementation
int main()
{
    // graph edges array.
    graphEdge edges[] = {
        // (x, y, w) -> edge from x to y with weight w
        {0,1,2},{0,2,4},{1,4,3},{2,3,2},{3,1,4},{4,3,3}
    };
    int N = 6;      // Number of vertices in the graph
    // calculate number of edges
    int n = sizeof(edges)/sizeof(edges[0]);
    // construct graph
    DiaGraph diagraph(edges, n, N);
    // print adjacency list representation of graph
    cout<<"Graph adjacency list "<<endl<<"(start_vertex, end_vertex, weight):"<<endl;
    for (int i = 0; i < N; i++)
    {
        // display adjacent vertices of vertex i
        display_AdjList(diagraph.head[i], i);
    }
    return 0;
}

Source: www.softwaretestinghelp.com

Add Comment

0

All those coders who are working on the C++ based application and are stuck on how to make graphs in c++ can get a collection of related answers to their query. Programmers need to enter their query on how to make graphs in c++ related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about how to make graphs in c++ for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of how to make graphs in c++ while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "how to make graphs in c++". Visit this developer's friendly online web community, CodeProZone, and get your queries like how to make graphs in c++ resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "how to make graphs in c++"

View All C++ queries

C++ queries related to "how to make graphs in c++"

Browse Other Code Languages

CodeProZone