C ++中的邻接矩阵

时间:2013-10-05 16:28:04

标签: c++ adjacency-matrix

我还需要在C ++中创建一个邻接矩阵,用于从像这样的文件中读取数据

ABC CDE 100
ZXY ABC  25
TER ZXY  11
POP ABC  66


     ABC CDE POP TER ZXY 
 ABC     100
 CDE
 POP 66
 TER                  11
 ZXY 25

#include <fstream>   // for std::ifstream
#include <sstream>   // for std::istringstream
#include <cstring>    // for std::string and std::getline
#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include<algorithm>
#include <string.h>

using namespace std;

#define MAX 30
#define WORD 3
string* currentArray;

typedef struct node{
int nodeId;
string destCity[MAX];
string arrCity[MAX];
int time;
}NODE;

typedef struct edge{
int adjoin;
int distance;
}EDGE;

typedef struct graph{
 NODE cityNode[MAX];
EDGE e[MAX][MAX];
}GRAPH;

GRAPH graf;


bool removeDuplicates(int count,string* tempArray){
    for (int i = 0; i <= count; i++)
{
    int n=0;
    bool matching = false;
    for (int j = 0; (j < i) && (matching == false); j++){
        if (currentArray[i] == currentArray[j]) matching = true;
    }
     //if (!matching) tempArray[n] = currentArray[i];
       }
 return false;  
}
void MergeA(int low ,int mid , int high)
{
int i = low, j = mid+1 , k = low;
string Temp[MAX];

while(i <= mid && j <= high)
{
    if( currentArray[i] <= currentArray[j] )
    {

        Temp[k].assign(currentArray[i]);
        i++;
    }
    else
    {
        Temp[k].assign(currentArray[j]);
        j++;
    }
    k++;
}
if(i > mid )
{
    for(int h = j ;h <= high ; h++ )
    {

        Temp[k].assign(currentArray[h]);
        k++;

    }
}
else
    for(int h = i; h<= mid ; h++ )
    {

        Temp[k].assign(currentArray[h]);
        k++;

    }
 for(int i = low; i <= high ; i++)
 {          currentArray[i].assign(Temp[i]);
  }
}
 void MergeSortA(int low , int high)
{

int mid = 0;
if(low < high)
{
    mid = low + (high-low)/2;
    MergeSortA(low , mid);
    MergeSortA(mid+1,high);
    MergeA(low,mid,high);

}
}

int main()
{


std::ifstream infile("theWords.txt");
    std::string line;
string departureCity[MAX],arrivalCity[MAX];
int time[MAX];
int count = 0;
    while (std::getline(infile,(line)) && count<30){
    std::istringstream iss(line);
            if ((iss) >> departureCity[count] >> arrivalCity[count] >> time[count]){
            //departureCityCopy[i]
                    std::cout << "From : " << departureCity[count] << " To : " << arrivalCity[count] << " Duration " << time[count] << "\n";
            graf.cityNode[count].destCity = departureCity[count];

            count++;
            }else{
                 // error processing that line
            }


}

std::string cpyDepartureCity[MAX],cpyArrivalCity[MAX];
int cpyTime[MAX];
std::copy(departureCity,departureCity+count,cpyDepartureCity);
std::copy(arrivalCity,arrivalCity+count,cpyArrivalCity);
std::copy(time,time+count,cpyTime);
currentArray= cpyDepartureCity;
MergeSortA(0,count);
cout<<"before dup"<<endl;
//removeDuplicates(count,&currentArray[1]);

for(int i = 0; i <= count ; i++){
    cout << cpyDepartureCity[i] <<endl;
}
currentArray= cpyArrivalCity;
MergeSortA(0,count);

/*for(int i = 0; i <= count ; i++){
    cout << cpyArrivalCity[i] <<endl;
}*/






}

我正在读取一个数组,然后使用合并排序按字母顺序对它们进行排序 我创建了一个节点,但我不确定如何从这里开始 我已经好几个小时都没有成功 请帮忙

1 个答案:

答案 0 :(得分:0)

我不打算给你写完整的代码,你应该自己学习。

这里描述算法:

1) Create 2D matrix (int type)

2) Create look-up table (hash map <String, int>)

3) Fill look-up table with your 3-letter strings and assign each of them ID (0 ... max count of strings)

4) Filling matrix - use look-up table int id = [XYZ] ... and then in your matrix, use this id as key to col / row -> put value into matrix