oriented and unoriented graphs c 2b 2b

Solutions on MaxInterview for oriented and unoriented graphs c 2b 2b by the best coders in the world

showing results for - "oriented and unoriented graphs c 2b 2b"
Iyad
27 Jan 2020
1#include <iostream>
2using namespace std;
3// stores adjacency list items
4struct adjNode {
5    int val, cost;
6    adjNode* next;
7};
8// structure to store edges
9struct graphEdge {
10    int start_ver, end_ver, weight;
11};
12class DiaGraph{
13    // insert new nodes into adjacency list from given graph
14    adjNode* getAdjListNode(int value, int weight, adjNode* head)   {
15        adjNode* newNode = new adjNode;
16        newNode->val = value;
17        newNode->cost = weight;
18         
19        newNode->next = head;   // point new node to current head
20        return newNode;
21    }
22    int N;  // number of nodes in the graph
23public:
24    adjNode **head;                //adjacency list as array of pointers
25    // Constructor
26    DiaGraph(graphEdge edges[], int n, int N)  {
27        // allocate new node
28        head = new adjNode*[N]();
29        this->N = N;
30        // initialize head pointer for all vertices
31        for (int i = 0; i < N; ++i)
32            head[i] = nullptr;
33        // construct directed graph by adding edges to it
34        for (unsigned i = 0; i < n; i++)  {
35            int start_ver = edges[i].start_ver;
36            int end_ver = edges[i].end_ver;
37            int weight = edges[i].weight;
38            // insert in the beginning
39            adjNode* newNode = getAdjListNode(end_ver, weight, head[start_ver]);
40             
41                        // point head pointer to new node
42            head[start_ver] = newNode;
43             }
44    }
45      // Destructor
46     ~DiaGraph() {
47    for (int i = 0; i < N; i++)
48        delete[] head[i];
49        delete[] head;
50     }
51};
52// print all adjacent vertices of given vertex
53void display_AdjList(adjNode* ptr, int i)
54{
55    while (ptr != nullptr) {
56        cout << "(" << i << ", " << ptr->val
57            << ", " << ptr->cost << ") ";
58        ptr = ptr->next;
59    }
60    cout << endl;
61}
62// graph implementation
63int main()
64{
65    // graph edges array.
66    graphEdge edges[] = {
67        // (x, y, w) -> edge from x to y with weight w
68        {0,1,2},{0,2,4},{1,4,3},{2,3,2},{3,1,4},{4,3,3}
69    };
70    int N = 6;      // Number of vertices in the graph
71    // calculate number of edges
72    int n = sizeof(edges)/sizeof(edges[0]);
73    // construct graph
74    DiaGraph diagraph(edges, n, N);
75    // print adjacency list representation of graph
76    cout<<"Graph adjacency list "<<endl<<"(start_vertex, end_vertex, weight):"<<endl;
77    for (int i = 0; i < N; i++)
78    {
79        // display adjacent vertices of vertex i
80        display_AdjList(diagraph.head[i], i);
81    }
82    return 0;
83}
84
queries leading to this page
cpp build adjusting list and matrix from directed unwieghted graphhow to implement graph in c 2b 2bc 2b 2b graphbuilding a graph in c 2b 2bwrite a c 2b 2b program to implement graph using adjacency list how to store graphs cppgraph using adjacency list in cc 2b 2b graph linksgraph cppmake graph c 2b 2bcreate a graph with vertices and edges in c 2b 2bedge list c 2b 2bimplementation of graph using adjacency list c 2b 2bc 2b 2b adjacency matrix and listgraphs in cppcreate a graph in c 2b 2b 3fhow to declare a graph c 2b 2bimplementing a graph in c 2b 2bgraphs in c 2b 2b stlgraph implementation c 2b 2bcpp build adjacency list from undirected graphcreate graph c 2b 2bhow to make graph in c 2b 2bhow to represent a graph adjacency matrix adjacency list a and b both nonehow to make graph c 2b 2bhow to add a edge into a linked list using c languageimpleentation of graph in cpp whit edgesways to implement a graph in c 2b 2bcpp build adjacency list using directed graphsbasic imoemention og graph in c 2b 2bgraph code c 2b 2bplot graphs in cppweighted graph type class c 2b 2bc 2b 2b has edge graphgraphs data structures code c 2b 2bgraph implementation c 2b 2b adjacency listhow to implement a graph in c 2b 2bgraph adt c 2b 2b create a graph c 2b 2bcode to create a graph in c 2b 2bhow to draw graphs in c 2b 2bc 2b 2b implementation file for a simple graph implemented as an adjacency setdirected graph using adjacency list in c 2b 2bcreateiing a graph c 2b 2boriented and unoriented graphs c 2b 2b