Bienvenido a nuestro sitio web, ahora hallarás la solucíon de lo que buscas.
Ejemplo: grafo c ++
//Code by Soumyadeep Ghosh insta- @soumyadepp//linked in : https://www.linkedin.com/in/soumyadeep-ghosh-90a1951b6///Basic implementation of undirected graph using OOP#include usingnamespace std;//undirected graphclassgraph
vector<int>*adjacency_list;//array of vectors to store adjacency listint Vertices;public://constructorgraph(int n)
Vertices=n;
adjacency_list=new vector<int>[Vertices];//dynamic allocationvoidadd_edge(int,int);voiddisplay_graph();;intmain()
graph g1(5);//graph of 5 vertices indices- 0 to 4//adding edges
g1.add_edge(0,1);//connect node number 0 to node number 1
g1.add_edge(1,2);//connect node number 1 to node number 2
g1.add_edge(1,3);//connect node number 1 to node number 3
g1.add_edge(2,4);//connect node number 2 to node number 4
g1.add_edge(2,3);//connect node number 2 to node number 3//displaying the graph
cout<<"The entered Graph is "<<endl;
g1.display_graph();return0;//function definitionsvoid graph::add_edge(int u,int v )void graph::display_graph()for(int i=0;i<Vertices;i++)
cout<<"Adjacency list of vertex of vertex "<<i<<endl;for(auto it:adjacency_list[i])//traverse through each list
cout<<it<<" ";
cout<<endl;//thank you!
Sección de Reseñas y Valoraciones
Recuerda que te concedemos valorar este artículo .
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)