/********************************************************************************************** * Program Name: graphType.h * Author: Zhixiang Chen * Course: CSCI/CMPE 3333, Fall 2014 * Lab 8: Header file for Lab 8 * Date: 08/2014 * Description: This header contains a graph, emphasizing on depth-first search * Graph representation: Adjacent list is used. And the input graph is weighted. * Input fiel format: Raw data of a graph is stored in a file. The first raw has * the number of nodes. the next row gives the adjacent list of the first * node, the third gives the adjacent list of the second node, and so on. ***********************************************************************************************/ #include #include #include #include #include #include #include "arrayBasedListType.h" #include "linkedListType.h" #include "stackType.h" using namespace std; #ifndef GRAPH #define GRAPH enum statusType{VISITED, UNVISITED, ACTIVE}; /************************************************************ graph node type *************************************************************/ struct vertexType { friend ostream & operator<<(ostream & os, vertexType n); int label; //the lable of the node statusType tag; //indicate VISITED, ACTIVE, UNVISITED status bool operator==(vertexType & rhs){return label == rhs.label;} vertexType & operator=(vertexType &); vertexType(); }; ostream & operator<<(ostream & os, vertexType n) { os<> adjList; //store the adjacent list arrayListType nodes; }; /*-------------------------------------------------------------------------- This the depth-first-search function. The inpput parameter indicates the starting node. --------------------------------------------------------------------------*/ void graphType::dfs(int start) { int i, j; bool more= false; arrayStackType stack; vertexType nodeTmp; adjVertexType adjNodeTmp; stack.push(nodes[start]); while(!stack.isEmpty()) { nodeTmp = stack.pop(); //pop the top stack node and visit it cout<> len; setSize(len); for (i=0; i>v.label; v.tag = UNVISITED; nodes.insertLast(v); inFile>>deg; for (j=0; j>newLabel>>newWeight; newNode.label = newLabel; newNode.weight = newWeight; adjList[i].insertLast(newNode); } adjList.setCount(); } } /*------------------------------------------------ The output file format is the same as that of the input file format --------------------------------------------------*/ void graphType::saveGraph(ofstream & outFile) { int len, i; adjVertexType newNode; len = nodes.length(); for (i=0; i