C 图数据结构

C++ Graph data Structure

本文关键字:数据结构      更新时间:2023-10-16

我在C 中编写了一个图形实现,其中城市是顶点,从一个城市到另一个城市的航班代表一个边缘,权重是这些城市之间的距离。顶点,边缘和权重存储在文件中,当程序运行时,将顶点,边缘和权重加载到程序中。我正在使用一个代表边缘的邻接矩阵。

现在,当程序运行时,它将提示用户:

  1. 选择出发城
  2. 退出。选项两个刚刚终止了程序。

如果用户选择选项ONE,则将列出文件中的所有城市。我选择了一个七个城市。因此看起来像1.)洛杉矶2.)纽约3.)迈阿密等直到选项7。当用户选择选项时,它将列出除用户选择的出发城市以外的所有目标城市。一旦用户选择他的目的地城市,将会有三种可能性。

现在,第一个可能性将没有直接的,或者通过城市A和城市B之间的联系,并且该计划将输出,[出发城市]和[目的地城市]之间没有目的地,请按任何钥匙返回。用户按任何键,菜单将再次显示。第二种可能性是,如果城市之间存在直接联系,那么该计划将输出[出发城市] - [目的地城市] = [Miles]与城市之间的里程之间的直接连接,或者没有直接连接将说没有直接连接,用户可以返回菜单。

第三种可能性是将有一个连接,它将显示出离开的城市和目的地城市,它们之间的所有城市以及它们之间的总里程,用户可以按任何键以返回到菜单。

现在,问题i" M的问题是从文件中获取信息,我无法弄清楚如何从文件中获取信息或如何编写文件,因此程序知道哪些是顶点,边缘和权重。此外,如何展示城市并通过连接或根本没有连接具有直接连接的城市。

include <iostream>
#include <fstream>
#pragma once
const int NULL_EDGE = 0;
typedef std::string String;
class GraphType
{
private:
    int edges[50][50];
    int m_numVertices;
    int m_maxVertices;
    int m_distance;
    String* m_vertices;
    bool* marks; // marks[i] is the mark for vertices[i]
    int IndexIs(String*, String);
public:
    GraphType();
    ~GraphType();
    bool isEmpty() const;
    bool isFull(); //to do
    int GetWeight(String, String); // to do
    void ClearMarks(); // to do
    void MarkVertex(String) // to do
    bool isMarked(String) // to do
    void addVertex(String);
    void addEdge(String, String, int);
    void displayCities();
};

#include "GraphType.h"
GraphType::GraphType()
{
    m_maxVertices = 50;
    m_distance = 0;
    m_vertices = new String[m_maxVertices];
    marks = new bool[50];
    std::ifstream loadFile;
    loadFile.open("load.txt");
    if (loadFile.fail())
        std::cout << " Error opening load.txtn";
    else
    {
        //stuck here
    }
    loadFile.close();
}
GraphType::~GraphType()
{
    delete[] m_vertices;
    delete[] marks;
}
int GraphType::IndexIs(String* vertices, String vertex)
{
    int index = 0;
    while (!(vertex == m_vertices[index]) == 0)
        index++;
    return index;
}
void GraphType::addVertex(String vertex)
{
    m_vertices[m_numVertices] = vertex;
    for (int i = 0; i < m_numVertices; i++)
    {
        edges[m_numVertices][i] = NULL_EDGE;
        edges[i][m_numVertices] = NULL_EDGE;
    }
    m_numVertices++;
}
void GraphType::addEdge(String startVertex, String destVertex, int weight)
{
    int row;
    int col;
    row = IndexIs(m_vertices, startVertex);
    col = IndexIs(m_vertices, destVertex);
    edges[row][col] = weight;
}
void GraphType::displayCities()
{
    //stuck here
}
bool GraphType::isEmpty() const
{
    return (m_numVertices == 0);
}
#include "GraphType.h"
int FlyMenu();
void CitiesMenu(GraphType&);
int main()
{
    int choose;
    GraphType gt;
    do
    {
        choose = FlyMenu();
        switch (choose)
        {
            case 1: CitiesMenu(gt);
            break;
            case 2:
            break;
            default: std::cout << " Invalid Inputn";
            break;
        }
    } while (choose != 2);
    return 0;
}
int FlyMenu()
{
    int option;
    std::cout << " 1.) Choose Depature Cityn";
    std::cout << " 2.) Exitn";
    std::cout << " Enter option: ";
    std::cin >> option;
    return option;
}
void CitiesMenu(GraphType& gt)
{
    gt.displayCities();
}

我知道深度遍历和宽度遍历算法,以查看城市之间是否存在联系,但我不知道如何在这种情况下实施它们。我不能使用标准模板库,只有std :: vector。我正在考虑写另一堂课,但我不知道那堂课会对我有什么帮助。

从我得到的东西中有两个问题:

  1. 从文件中读取/写入:最简单的解决方案将是频率的。

freopen("input","r",stdin) 
freopen("output","w",stdout)

现在,每个cin & cout操作都将在您在freopen

上定义的文件上完成
  1. 实施DFS/BFS:我将为您编码最简单的DFS类型,并且您必须对其进行编辑以进行编码。

bool visit[MAX_CITY + 10];
void DFS(int x){
    visit[x] = 1;
    for (int i=1;i<=MAX_CITY;i++){
        int nx = i;
        if (adj[x][i] == -1) continue; // a cost of -1 means no flight
        if (visit[nx]) continue;
        DFS(nx);
    }
}