CLRS 22 3 2,7 鄰接表實現

2021-08-20 05:05:00 字數 3846 閱讀 3848

// stdafx.h : 標準系統包含檔案的包含檔案,

// 或是經常使用但不常更改的

// 特定於專案的包含檔案

//#pragma once

#include "targetver.h"

#include #include #include#include#include#include#include#include#include#include#include#include#include// todo: 在此處引用程式需要的其他標頭檔案

// aiqiyi.cpp: 定義控制台應用程式的入口點。

//#include "stdafx.h"

using namespace std;

int times = 0;

class node

};void dfs_visit(vector>& node_next, vector& node_set, int target)

} times++;

node_set[target]->end_time = times;

}void dfs(vector>& node_next, vector& node_set)

}int main()

(node_next[0]).push_back(node_set[2]);

(node_next[0]).push_back(node_set[3]);

(node_next[0]).push_back(node_set[6]);

(node_next[1]).push_back(node_set[4]);

(node_next[1]).push_back(node_set[8]);

(node_next[2]).push_back(node_set[5]);

(node_next[3]).push_back(node_set[7]);

(node_next[3]).push_back(node_set[8]);

(node_next[4]).push_back(node_set[8]);

(node_next[5]).push_back(node_set[6]);

node_next[6].push_back(node_set[2]);

(node_next[7]).push_back(node_set[9]);

(node_next[8]).push_back(node_set[0]);

(node_next[9]).push_back(node_set[7]);

dfs(node_next,node_set);

for (int i = 0; i < 10; i++)

cout << node_set[i]->start_time<<" "return 0;

}

void dfs_noncursion(vector>& node_next, vector& node_set)

else

int sign = 1;

for (int j = 0; j < node_next[top_rank - 'q'].size(); ++j)

}if (sign)

}} }

}

以下為嘗試用clrs上的dfs與拓撲排序檢查有向圖中是否存在環。

// aiqiyi.cpp: 定義控制台應用程式的入口點。  

//

#include "stdafx.h"

using namespace std;

static int times = 0;

static bool sign = 0;

class node

};int find_index(vector& node_set, int targets)

}int dfs_visit(vector> node_next, vectornode_set, int targets)//targets is the value of node!!

} times++;

node_set[target]->end_time = times;

return 0;

}int dfs(vector> node_next, vectornode_set)

} return 0;

}static const bool less(const int &a, const int & b)

bool canfinish(int numcourses, vector>& prerequisites)

int pair_len = prerequisites.size();

for (int i = 0; i < pair_len; ++i)//to generate the graph

for (int i = 0; i < numcourses; ++i)

//we need to check if it's an dag first--to check if there exists a strongly-connectedly-component

dfs(node_next,node_set);

//to record the finish time of each node_visit in the first round of dfs;the info is useful for the next round dfs

vectorfin_time;

for (int i = 0; i < numcourses; ++i)

fin_time.push_back(node_set[i]->end_time);

//release memory!!!

vector().swap(node_set);

vector>().swap(node_next);

//to compare the vector before and after sort, then you'll know the rank of numbers in the vector

//here maybe you can use priority queue

vectorfin_time_i = fin_time;

sort(fin_time.begin(), fin_time.end(),less);

vectorindex;

for (int i = 0; i < numcourses; ++i)

//sort node_set_t and node_next_t in the visiting order

vector>node_next_r;

vectornode_set_r;

for (int i = 0; i < numcourses; ++i)

sign = 1;

times = 0;

int res = !dfs(node_next_r, node_set_r);

//for (int i = 0; i < numcourses; ++i)

// for (int i = 0; i < numcourses; ++i)

delete node_set_t[i];

vector().swap(node_set_t);

vector().swap(node_set_r);

vector>().swap(node_next_t);

vector>().swap(node_next_r);

return res;

}int main()

); pre.push_back();

cout

}

鄰接表實現

鄰接表構建 鄰接表某個頂點遍歷 以上 include include include include using namespace std class adjacencytable void setedge int s,int e,int weight void getvertexinfo int...

陣列實現鄰接表

之前我們介紹過圖的鄰接矩陣儲存法,它的空間和時間複雜度都是n 2,現在我來介紹另外一種儲存圖的方法 鄰接表,這樣空間和時間複雜度就都是m。對於稀疏圖來說,m要遠遠小於n 2。先上資料,如下 第一行兩個整數n m。n表示頂點個數 頂點編號為1 n m表示邊的條數。接下來m行表示,每行有3個數x y z...

C Dijkstra鄰接表實現

ifndef mapimplement h included define mapimplement h included include include define maxvertexnum 100 define infinity 65535 using namespace std typede...