// age " // !!!!!!!!!!!!!!! " // ro az comment darbiyarin , yalhaye graph do tarafe mishan #include #include #include using namespace std; struct GraphEdge { int from; int to; int value; GraphEdge(int f,int t,int v) : from(f) , to(t) , value(v) {} ; }; class Graph { public : list* List; int E; int V; Graph(int v) { V = v; E = 0; List = new list[V]; } void AddEdge(int f,int t,int v) { List[f].push_back( GraphEdge(f,t,v) ); // List[t].push_back( GraphEdge(t,f,v) ); // !!!!!!!!!!!!!!! E++; } void Print() { cout<<"\n"; for (int i=0 ; i::iterator j=List[i].begin() ; j!=List[i].end() ; j++) // if ( j->from <= j->to ) // Prevent of print duplicate edges // !!!!!!!!!!!!!!! cout<< j->from <<" , "<< j->to << " = "<< j->value <<"\n"; } bool* visited; void DFS(int v) { visited = new bool[V]; for (int i=0 ; i::iterator j=List[v].begin() ; j!=List[v].end() ; j++) if ( !visited[ j->to ] ) { visited[ j->to ] = true; cout<from<<" , "<< j->to<<" = "<value<<"\n"; WH_DFS( j-> to ); } } }; int main() { Graph g(7); g.AddEdge(0,5,10); g.AddEdge(0,1,28); g.AddEdge(1,6,14); g.AddEdge(1,2,16); g.AddEdge(2,3,12); g.AddEdge(3,4,22); g.AddEdge(3,6,18); g.AddEdge(4,5,25); g.AddEdge(4,6,24); g.Print(); g.DFS(0); cin.get(); return 0; }