C++ Creating Array of Pointers to Nodes in Directed Graph

graph implementation in c
array of pointers in c
graph data structure c++ source code
graph implementation c++ adjacency list
adjacency list representation of graph in c
array of pointers in c++
adjacency list directed graph
directed graph implementation java

I'm trying to create a directed graph represented by an array of pointers to nodes, but I'm struggling to add nodes into each index of the graph. Here is what I have:

struct Node {
    int index;                 
    list<Node*> outgoingNodes; 
};

struct Graph {
    Node* nodePointers; // Array of pointers to nodes in graph
    int N;              // Number of nodes in graph       
};

Here is how I am creating the graph:

Graph* graph = new Graph();
graph->N = 7;
graph->nodePointers = new Node[graph->N];

I then try to add a node into index 0 in the graph in the following way, but I get an error that "operand types are 'Node' and 'Node*'":

Node* a = new Node();
a->index = 0;
graph->nodePointers[0] = a;

Without changing either of my structs, how could I correctly add a node into an index in my graph's array of node pointers?

Thanks for any help!

Node* nodePointers is a pointer to an array of Nodes. If you want an array of Node pointers, you need to declare it as Node** nodePointers, and allocate the array with new Node*[graph->N]:

struct Graph {
    Node** nodePointers; // Array of pointers to nodes in graph
    int N;              // Number of nodes in graph       
};

int main() {
    Graph* graph = new Graph();
    graph->N = 7;
    graph->nodePointers = new Node*[graph->N];
    ...
}

Implement Graph Data Structure in C, Below is C implementation of a directed graph using Adjacency list: #include <​stdio.h> struct Graph {. // An array of pointers to Node to represent adjacency list Function to create an adjacency list from specified edges. struct Graph*  Weighted Directed Graph Implementation –. In a weighted graph, each edge will have weight (or cost) associated with it as shown below: Below is C implementation of a weighted directed graph using Adjacency list. The implementation is similar to that of unweighted directed graph, except we’re also storing weight info along with every edge.

First:

struct Node {
    int index;                 
    list<Node*> outgoingNodes; 
};

Although correct, it is inefficient for no apparent reason. Almost always prefer a vector over a list. It is as easy to work with, but takes less memory and works faster on almost any conceivable use case:

struct Node {
    int index;                 
    std::vector<Node*> outgoingNodes; 
};

Next, the code:

struct Graph {
    Node* nodePointers; // Array of pointers to nodes in graph
    int N;              // Number of nodes in graph       
};

Holds a block of Node objects, not pointers to nodes. The best thing is to use a vector of pointers:

struct Graph {
    std::vector<std::unique_ptr<Node>> nodePointers; // pointers to nodes in graph

};

This way deallocation and memory management will be automatic.

Then your usage example becomes:

// are you sure graph has to be on the heap?
auto graph = std:: make_unique<Graph>();
graph->nodePointers.resize(7);

I then you can add a node into index 0 in the graph in the following way:

graph->nodePointers[0] = std::make_unique<Node>();
graph->nodePointers[0]->index = 0;


This was the better way to do it, but if you insist on:

Without changing either of my structs, how could I correctly add a node into an index in my graph's array of node pointers?

Then you should note that "graph's array" is not made of pointers, but of nodes. So adding nodes is done differently:

// you can still allocate the graph on the heap, but the following way is safer
Graph graph;
graph.N = 7;
graph.nodePointers = new Node[graph.N];

But now nodePointers is a misnomer, because it should be named nodes (not pointers).

Then add a node into index 0 in the graph in the following way (by this point it is already constructed):

graph->nodePointers[0].index = 0;

And adding an edge looks lije:

graph->nodePointers[0].outgoingNodes.push_back(&graph->nodePointets[2]);

Graph Implementation in C++ (without using STL), int N; // number of nodes in the graph. public: // An array of pointers to Node to represent. // adjacency list. Node **head;. // Constructor. Graph(Edge edges[], int n  Graph Implementation in C++ (without using STL) Given an undirected or a directed graph, implement the graph data structure without using any container provided by any programming language library (e.g. STL in C++ or Collections in Java, etc). Implement for both weighted and unweighted graphs using Adjacency List representation.

At line graph->nodePointers[0] = a; change this to graph->nodePointers[0] = *a; It will work.

Now let me explain you, Suppose you want an array of int then you can declare it as int x[10] or int *x=new int(10). What it shows in second case that x is an pointer which points to int object not to int pointer. I hope you got your solution.

Difference between pointer to an array and array of pointers , Introduction · Lexical Analysis · Syntax Analysis · Syntax Directed Translation · Runtime Nested Loops in C with Examples · How to create GUI in C programming using GTK We can likewise declare a pointer that can point to whole array rather than just a The above declaration is the pointer to an array of five integers. Graph implementation using C++ . GitHub Gist: instantly share code, notes, and snippets.

Engineering Technology and Applications, Figure 1, the corresponding directed-graph is expressed as {(B, D), (C, D), (A, B), (A, node will be created only once, and their reference (the pointers) are actually (1) Create an array named Array as a global variable to store all the nodes. In the above graph, A, B, C, and D are the vertices of the graph. Edge: The link or path between two vertices is called an edge. It connects two or more vertices. The different edges in the above graph are AB, BC, AD, and DC. Adjacent node: In a graph, if two nodes are connected by an edge then they are called adjacent nodes or neighbors. In the above graph, vertices A and B are connected by edge AB.

Mathematical Structures for Computer Science, The adjacency matrix for an undirected graph is given in lower triangular form by Draw the Describe the adjacency matrix for Kn, the simple, complete graph with n nodes. (A pointer takes one storage location.) c. How many storage locations would be For the directed graph of Exercise 40, construct the array-​pointer  Graph and its representations. Graph is a data structure that consists of following two components: 1. A finite set of vertices also called as nodes. 2. A finite set of ordered pair of the form (u, v) called as edge. The pair is ordered because (u, v) is not same as (v, u) in case of a directed graph(di-graph).

Pointer to an Array, Declare a C/C++ function returning pointer to array of integer pointers · Pointer to an Array | Array Pointer The following figure shows the pointer p and ptr. C Programming - Find if there is a path between two vertices in a directed graph - check whether there is a path from the first given vertex to second. Given a Directed Graph and two vertices in it, check whether there is a path from the first given vertex to second.

Comments
  • Graph* graph = new Graph(); -- Why not simply Graph graph;?
  • That would work just as well, but it doesn't look like that solves the issue of adding a node into an index in the graph's array of pointers
  • issue of adding a node -- Your attempt is not adding a Node -- it is attempting to add a Node *, and that is wrong. So before an answer, what are you trying to accomplish? Is it to utilize an array of Node *, or an array of Node? If it's the former, your declaration in Graph is wrong. If it's the latter, then add a Node and not Node *.
  • I'm hoping to utilize an array of Node*, though correcting my declaration of graph to "Graph graph" doesn't seem to solve the issue. Is there a way to utilize an array of Node* while keeping my declaration of graph as a pointer to graph?
  • My point of Graph graph; is to emphasize the need to not allocate memory. C++ is not Java or other languages where every object needs to be created with new.