Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

有向圖的十字鏈結串列:建圖、增刪弧與雙向遍歷輸出

編輯:關於 Android 程式設計

本文討論有向圖的十字鏈結串列(Orthogonal List)表示法,涵蓋建圖、新增弧、刪除弧,以及以鄰接表格式輸出圖形(分為兩種視角:同尾弧串成的鏈、同頭弧串成的鏈),以 C++ 描述。

一、資料結構設計

ArcBox(弧的節點)包含五個欄位:

  • 弧尾頂點序號(tailvex)
  • 弧頭頂點序號(headvex)
  • 指標域 hlink:指向與這條弧同頭的下一條弧
  • 指標域 tlink:指向與這條弧同尾的下一條弧
  • 弧的資訊 info(權重、距離等)

VexNode(頂點節點)包含三個欄位:

  • 頂點名稱(v1、v2……)
  • 指標域 firstin:指向以該頂點為弧頭的第一條弧
  • 指標域 firstout:指向以該頂點為弧尾的第一條弧

十字鏈結串列的精髓就在這裡:每條弧同時掛在兩條鏈上,一條串起同尾的弧,一條串起同頭的弧。因此它同時具備鄰接表與逆鄰接表的優點,找入弧與找出弧都是 O(1) 起點。

二、完整程式碼

class ArcBox {
public:
    ArcBox();
    ArcBox(int from, int to, int inf);
    ~ArcBox() {};
    friend class OLGraph;
    int getTailvex() { return tailvex; }
    int getHeadvex() { return headvex; }
    ArcBox *getHlink() { return hlink; }
    ArcBox *getTlink() { return tlink; }
    int getInfo() { return info; }
    void setInfo(int in) { info = in; }
    void setTailvex(int tv) { tailvex = tv; }
    void setHeadvex(int hv) { headvex = hv; }
    void setHlink(ArcBox *hl) { hlink = hl; }
    void setTlink(ArcBox *tl) { tlink = tl; }
private:
    int tailvex;
    int headvex;
    ArcBox *hlink;   // 同頭的下一條弧
    ArcBox *tlink;   // 同尾的下一條弧
    int info;        // 權重、距離……
};

ArcBox::ArcBox() {
    tailvex = -1;
    headvex = -1;
    hlink = NULL;
    tlink = NULL;
    info = -1;
}

ArcBox::ArcBox(int tail, int head, int inf) {
    tailvex = tail;
    headvex = head;
    info = inf;
    hlink = NULL;
    tlink = NULL;
}

class Vexnode {
public:
    Vexnode();
    ~Vexnode() {};
    friend class OLGraph;
    void setNodeNum(int noden) { nodeNum = noden; }
    void setFirstin(ArcBox *firsti) { firstin = firsti; }
    void setFirstout(ArcBox *firsto) { firstout = firsto; }
    int getNideNum() { return nodeNum; }
    ArcBox *getFirstin() { return firstin; }
    ArcBox *getFirstout() { return firstout; }
private:
    int nodeNum;
    ArcBox *firstin;    // 第一條入弧
    ArcBox *firstout;   // 第一條出弧
};

Vexnode::Vexnode() {
    nodeNum = 0;
    firstin = NULL;
    firstout = NULL;
}

class OLGraph {
public:
    OLGraph();
    ~OLGraph();
    int getVexnum() { return vexnum; }
    int getArcnum() { return vexnum; }
    Vexnode getVList(int vexIndex) { return xList[vexIndex]; }
    // 參數 tail 與 head 為弧尾與弧頭的「資料」而非編號
    int addArc(int tail, int head, int inf);
    int deleteArc(int tail, int head);
    int createOLGraph();
    int vexLoc(int vexNum);
    int ftPrint();   // 同尾弧構成鏈
    int tfPrint();   // 同頭弧構成鏈
private:
    Vexnode xList[MAX];
    int vexnum;
    int arcnum;
};

OLGraph::OLGraph() {
    vexnum = 0;
    arcnum = 0;
}
OLGraph::~OLGraph() { }

// 依頂點資料找出它在表頭陣列中的下標
int OLGraph::vexLoc(int vexNum) {
    for (int i = 0; i < this->vexnum; i++) {
        if (xList[i].nodeNum == vexNum) {
            return i;
        }
    }
    return -1;
}

// 以鄰接表格式輸出:每列沿 tlink 走,得到同尾的所有弧
int OLGraph::ftPrint() {
    ArcBox *tempArc = new ArcBox();
    for (int index = 0; index < this->vexnum; index++) {
        cout << index << " : ";
        tempArc = xList[index].firstout;
        while (tempArc) {
            cout << "--> [ " << tempArc->headvex << "," << tempArc->info << " ]";
            tempArc = tempArc->tlink;
        }
        cout << "--> ^" << endl;
    }
    cout << "-------------------" << endl;
    return 0;
}

// 以逆鄰接表格式輸出:每列沿 hlink 走,得到同頭的所有弧
int OLGraph::tfPrint() {
    ArcBox *tempArc = new ArcBox();
    for (int index = 0; index < this->vexnum; index++) {
        cout << index << " : ";
        tempArc = xList[index].firstin;
        while (tempArc) {
            cout << "--> [ " << tempArc->tailvex << "," << tempArc->info << " ]";
            tempArc = tempArc->hlink;
        }
        cout << "--> ^" << endl;
    }
    cout << "-------------------" << endl;
    return 0;
}

// 新增一條弧:同一個 ArcBox 同時掛上兩條鏈
int OLGraph::addArc(int tail, int head, int inf) {
    int tailIndex = vexLoc(tail);   // 弧尾在表頭陣列中的位置
    int headIndex = vexLoc(head);   // 弧頭在表頭陣列中的位置

    ArcBox *ftArc = new ArcBox(tailIndex, headIndex, inf);

    ftArc->hlink = xList[headIndex].firstin;   // 接入同頭鏈
    xList[headIndex].firstin = ftArc;

    ftArc->tlink = xList[tailIndex].firstout;  // 接入同尾鏈
    xList[tailIndex].firstout = ftArc;
    return 0;
}

// 刪除一條弧
int OLGraph::deleteArc(int tail, int head) {
    int tailIndex = vexLoc(tail);
    int headIndex = vexLoc(head);
    ArcBox *ftArc = new ArcBox(), *preArc = new ArcBox();

    // 先把它從同尾鏈摘下來
    ftArc = xList[tailIndex].firstout;
    if (ftArc && ftArc->headvex == headIndex) {
        xList[tailIndex].firstout = ftArc->tlink;
        // 注意:此處不可 delete,因為後面還要用這個物件走另一條鏈
    } else {
        preArc = ftArc;
        ftArc = preArc->tlink;
        while (ftArc) {
            if (ftArc->headvex == headIndex) {
                preArc->tlink = ftArc->tlink;
                break;
            } else {
                preArc = ftArc;
                ftArc = preArc->tlink;
            }
        }
    }

    // 再把它從同頭鏈摘下來,這時才可以釋放
    ftArc = xList[headIndex].firstin;
    if (ftArc && ftArc->tailvex == tailIndex) {
        xList[headIndex].firstin = ftArc->hlink;
        delete ftArc;
    } else {
        preArc = ftArc;
        ftArc = preArc->hlink;
        while (ftArc) {
            if (ftArc->tailvex == tailIndex) {
                preArc->hlink = ftArc->hlink;
                delete ftArc;
                break;
            } else {
                preArc = ftArc;
                ftArc = preArc->hlink;
            }
        }
    }
    return 0;
}

int OLGraph::createOLGraph() {
    cout << "請輸入節點數目:" << endl;
    cin >> this->vexnum;
    cout << "請輸入弧的數目:" << endl;
    cin >> this->arcnum;
    cout << "請輸入頂點資訊:" << endl;
    for (int i = 0; i < this->vexnum; i++) {
        cin >> this->xList[i].nodeNum;
    }
    cout << "請輸入弧的資訊" << endl;
    int tail, head, info;
    for (int i = 0; i < this->arcnum; i++) {
        cin >> tail >> head >> info;
        addArc(tail, head, info);
    }
    return 0;
}

int main() {
    OLGraph *olGraph = new OLGraph();
    olGraph->createOLGraph();
    olGraph->ftPrint();
    olGraph->tfPrint();

    cout << "添加邊(2,1,0)" << endl;
    olGraph->addArc(2, 1, 0);
    olGraph->ftPrint();
    olGraph->tfPrint();

    cout << "刪除邊(1,2)" << endl;
    olGraph->deleteArc(1, 2);
    olGraph->ftPrint();
    olGraph->tfPrint();
}

三、addArc 為什麼這樣寫

新增弧時,只建立一個 ArcBox,然後讓它同時接進兩條鏈:hlink 接上原來 headIndex 的 firstin,tlink 接上原來 tailIndex 的 firstout,最後把兩個表頭指標都指向這個新節點。採頭插法是因為它不需要遍歷到鏈尾,時間複雜度為 O(1)。

四、deleteArc 最容易犯錯的地方

刪除弧必須先在兩條鏈上把它摘掉,才能釋放記憶體。實務上最容易踩的雷是太早 delete:如果在處理完同尾鏈之後立刻 delete ftArc,接著要沿 hlink 走同頭鏈時就會存取到已釋放的記憶體。所以正確順序是——先從同尾鏈摘下但不釋放,再從同頭鏈摘下並釋放。程式碼中已用註解標出這一點。

五、實測結果

原圖:

新增邊(2,1,0):

刪除邊(1,2):

輸出結果:

十字鏈結串列相比單純的鄰接表,代價是每條弧多了一個指標、插入刪除時要維護兩條鏈;但收穫是可以直接以 O(1) 定位任意頂點的入弧與出弧,在拓撲排序、強連通分量等需要頻繁查詢入度的圖形演算法中,這個結構非常值得。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved