Skip to main content

Adjacency list representation of undirected graph in c++

C++ program for Adjacency list representation of undirected graph. Here problem description and explanation.

// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program for 
    Undirected graph representation by using adjacency list 
*/
class AjlistNode
{
	public:
	// Vertices node key
	int id;
	AjlistNode *next;
	AjlistNode(int id)
	{
		// Set value of node key
		this->id = id;
		this->next = nullptr;
	}
};
class Vertices
{
	public: int data;
	AjlistNode *next;
	AjlistNode *last;
	Vertices(int data)
	{
		this->data = data;
		this->next = nullptr;
		this->last = nullptr;
	}
	Vertices()
    {
        this->data = 0;
        this->next = nullptr;
        this->last = nullptr;
    }
};
class Graph
{
	public:
	// Number of Vertices
	int size;
	Vertices *node;
	Graph(int size)
	{
		// Set value
		this->size = size;
		this->node = new Vertices[size];
		this->setData();
	}
	// Set initial node value
	void setData()
	{
		if (this->size <= 0)
		{
			cout << "\nEmpty Graph" << endl;
		}
		else
		{
			for (int index = 0; index < this->size; index++)
			{
				this->node[index].data = index;
			}
		}
	}
	// Connect two nodes
	void connect(int start, int last)
	{
		AjlistNode *edge = new AjlistNode(last);
		if (this->node[start].next == nullptr)
		{
			this->node[start].next = edge;
		}
		else
		{
			// Add edge at the end
			this->node[start].last->next = edge;
		}
		// Get last edge 
		this->node[start].last = edge;
	}
	//  Handling the request of adding new edge
	void addEdge(int start, int last)
	{
		if (start >= 0 && start < this->size && 
            last >= 0 && last < this->size)
		{
			this->connect(start, last);
			if (start == last)
			{
				// When self loop
				return;
			}
			this->connect(last, start);
		}
		else
		{
			// When invalid nodes
			cout << "\nHere Something Wrong" << endl;
		}
	}
	void printGraph()
	{
		if (this->size > 0)
		{
			// Print graph ajlist Node value
			for (int index = 0; index < this->size; ++index)
			{
				cout << "\nAdjacency list of vertex " << index << " :";
				AjlistNode *temp = this->node[index].next;
				while (temp != nullptr)
				{
					// Display graph node 
					cout << "  " << this->node[temp->id].data;
					// Visit to next edge
					temp = temp->next;
				}
			}
		}
	}
};
int main()
{
	// 5 implies the number of nodes in graph
	Graph *g = new Graph(5);
	// Connect node with an edge
	g->addEdge(0, 1);
	g->addEdge(0, 2);
	g->addEdge(0, 4);
	g->addEdge(1, 2);
	g->addEdge(1, 4);
	g->addEdge(2, 3);
	g->addEdge(3, 4);
	// print graph element
	g->printGraph();
	return 0;
}

Output

Adjacency list of vertex 0 :  1  2  4
Adjacency list of vertex 1 :  0  2  4
Adjacency list of vertex 2 :  0  1  3
Adjacency list of vertex 3 :  2  4
Adjacency list of vertex 4 :  0  1  3




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment