編輯:關於 Android 程式設計
本文討論有向圖的十字鏈結串列(Orthogonal List)表示法,涵蓋建圖、新增弧、刪除弧,以及以鄰接表格式輸出圖形(分為兩種視角:同尾弧串成的鏈、同頭弧串成的鏈),以 C++ 描述。
ArcBox(弧的節點)包含五個欄位:

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

十字鏈結串列的精髓就在這裡:每條弧同時掛在兩條鏈上,一條串起同尾的弧,一條串起同頭的弧。因此它同時具備鄰接表與逆鄰接表的優點,找入弧與找出弧都是 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();
}
新增弧時,只建立一個 ArcBox,然後讓它同時接進兩條鏈:hlink 接上原來 headIndex 的 firstin,tlink 接上原來 tailIndex 的 firstout,最後把兩個表頭指標都指向這個新節點。採頭插法是因為它不需要遍歷到鏈尾,時間複雜度為 O(1)。
刪除弧必須先在兩條鏈上把它摘掉,才能釋放記憶體。實務上最容易踩的雷是太早 delete:如果在處理完同尾鏈之後立刻 delete ftArc,接著要沿 hlink 走同頭鏈時就會存取到已釋放的記憶體。所以正確順序是——先從同尾鏈摘下但不釋放,再從同頭鏈摘下並釋放。程式碼中已用註解標出這一點。
原圖:

新增邊(2,1,0):

刪除邊(1,2):

輸出結果:

十字鏈結串列相比單純的鄰接表,代價是每條弧多了一個指標、插入刪除時要維護兩條鏈;但收穫是可以直接以 O(1) 定位任意頂點的入弧與出弧,在拓撲排序、強連通分量等需要頻繁查詢入度的圖形演算法中,這個結構非常值得。
Android 本地 OTA 升級:用 RecoverySystem 實現 SD 卡一鍵升級
傳統的本地升級流程有點麻煩:把升級包拷到 SD 卡,重啟進 Recovery,手動選包、確認、等待。對普通用戶來說,這套操作簡直像拆機。 其實這一切都可以封裝成一個 Ap
Android UI 組件 AppWidget 控件入門詳解
AppWidget(應用小部件)是我們可以放到桌面、方便隨時操作的小組件。本文從原理到實踐,帶大家完整入門 AppWidget 的開發。 一、Widget 引入 我們可以
Genymotion 安裝配置全流程:告別龜速的官方模擬器
Eclipse 自帶的 SDK 模擬器啟動之慢,做過 Android 開發的人大概都深有體會。本文記錄 Genymotion 從註冊、安裝到成功啟動的完整過程,並附上兩個
深入 Android Dialog:八種常見彈窗效果的實作解析
Dialog 是 Android 裡使用頻率極高、變化也最多的元件。從最簡單的確認框,到列表、單選、多選、進度條與自訂版面,幾乎都能用 AlertDialog.Build