Saltar al contenido

ejemplo de código de lista de adyacencia de c ++ de implementación de gráfico dirigido

Hola usuario de nuestra web, hallamos la respuesta a lo que andabas buscando, continúa leyendo y la verás aquí.

Ejemplo: grafo ponderado c ++

//code  by Soumyadeep Ghosh//insta : @soumyadepp//linked in: https://www.linkedin.com/in/soumyadeep-ghosh-90a1951b6/#includeusingnamespace std;//undirected weighted graph and all functionsclassWeightedGraph
  vector< pair<int,int>>*adjacency_list;int vertices;public:WeightedGraph(int n)
    vertices=n;
    adjacency_list=new vector< pair<int,int>>[n];voidadd_edge(int v1,int v2,int wt);voiddfsHelper(int src,bool visited[]);voiddfs(int src);voidbfs(int src);intminDistance(vector<int>dist,bool visited[]);voiddjisktra(int src);voiddisplay_graph();;intmain()//graph of five vertices
  WeightedGraph wg1(5);//adding edges
  wg1.add_edge(0,1,10);
  wg1.add_edge(1,2,20);
  wg1.add_edge(2,3,30);
  wg1.add_edge(1,3,40);
  wg1.add_edge(2,4,100);
  wg1.add_edge(4,0,10);//displaying the graph
  wg1.display_graph();//dfs from vertex 0
  wg1.dfs(0);//bfs from vertex 0
  wg1.bfs(0);//djikstrafor(int i=0;i<5;i++)djikstra(i);return0;//function definitionsvoidWeightedGraph::add_edge(int v1,int v2,int wt)/*push the other vertex into the adjacency list of the given vertex
  and vice versa. If it would have been a directed graph,
  only the first line would be enough
  */
  adjacency_list[v1].push_back(make_pair(v2,wt));
  adjacency_list[v2].push_back(make_pair(v1,wt));voidWeightedGraph::dfsHelper(int src,bool visited[])
  visited[src]=true;
  cout<<src<<" ";for(vector<int>::iterator it=adjacency_list.begin();i!=adjacency_list.end();it++)if(!visited[it->first]);dfsHelper(it->first,visited);voidWeightedGraph::dfs(int src)bool visited[vertices];for(int i=0;i<vertices;i++)
    visited[i]=false;dfsHelper(src,visited);voidWeightedGraph::bfs(int src)bool visited[vertices];for(int i=0;i<vertices;i++)
    visited[i]=false;
  cout<<src<<" ";
  visited[src]=true;
  queue<int>helper;
  helper.push(src);while(!helper.empty())
    src=helper.front();for(vector<int>::iterator it=adjacency_list[src].begin();it!+adjacency_list[src].end();it++)if(!visited[it->first])
        visited[it->first]=true;
        cout<<it->first<<" ";
        helper.push(it->first);
    helper.pop();intWeightedGraph::minDistance(vector<int>dist,bool visited[])int min=INT_MAX;int minIndex=INT_MAX;for(int i=0;i<N;i++)if(!visited[i]&&dist[i]<=min)
        min=dist[i];
        minIndex=i;return minIndex;voidWeightedGraph::djikstra(int src)
    vector<int>dist;bool visited[vertices];for(int i=0;i<vertices;i++)
      dist.push_back(INT_MAX);
      visited[i]=false;
    visited[src]=true;
    dist[src]=0;for(int i=0;i<vertices-1;i++)int k=minDistance(dist,visited);
      visited[k]=true;for(int j=0;j<vertices;j++)if(!visited[i]&&dist[i]!=INT_MAX&&adjacency_list[i][j].second+dist[i]<dist[j])
          dist[j]=adjacency_list[i][j].second+dist[i];for(int i=0;i<dist.size();i++)
      cout<<dist[i]<<" ";
    
    cout<<endl;voidWeightedGraph::display_graph()int a,b;//first loop to traverse across verticesfor(int i=0;i<vertices;i++)
      cout<<"Adjacency list of vertex "<<i<<endl;//second loop to traverse across the adjacency list of some vertex ifor(auto it=adjacency_list[i].begin();it!=adjacency_list[i].end();it++)//set a as the vertex number and b as the weight
      a=it->first;
      b=it->second;
      cout<<"Vertex : "<<a<<" Weight : "<<b<<endl;
    cout<<endl;//thank you!

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *