graph.cpp

来自「Data Abstraction & Problem Solving with 」· C++ 代码 · 共 58 行

CPP
58
字号
// ****************************************************// Implementation file Graph.cpp // An adjacency list representation of an undirected,// weighted graph.  // ****************************************************#include "Graph.h"Graph::Graph(int n){   adjList.assign(n);   numVertices = n;}  // end constructorint Graph::getNumVertices() const{   return numVertices;}  // end getNumVerticesint Graph::getNumEdges() const{     return numEdges;}  // end getNumEdgesint Graph::getWeight(Edge e) const{   return e.weight;}  // end getWeightvoid Graph::add(Edge e){   int v = e.v,       w = e.w,       weight = e.weight;   adjList[v].insert(make_pair(w, weight));   adjList[w].insert(make_pair(v, weight));   numEdges++;}  // end addvoid Graph::remove(Edge e){   int v = e.v,       w = e.w,        weight = e.weight;   adjList[e.v].erase(w);   adjList[e.w].erase(v);   numEdges--;}  // end removemap<int,int>::iterator Graph::findEdge(int v, int w){   map<int,int> m = adjList[v];   map<int,int>::iterator iter = m.find(w);	   return iter;}  // end findEdge

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?