第7章 图
来源:互联网 发布:mac 实用软件下载 编辑:程序博客网 时间:2024/05/22 16:48
leetcode刷了一大半,数组,链表,树,队列和栈的题目已经做了好多,感觉比较扎实了。突然发现后面好多关于图算法的问题,没办法,又继续开始学习“图”这种数据结构。折腾了一天,把图的创建,深度优先级遍历(DFS)和广度优先级遍历(BFS)都用c++写出来了,算法参照了《算法导论》和《数据结构和算法》。
遍历是各种图操作的基本思路,所以一定要熟练掌握。
注:
1. 图的表示采用了邻接链表,因为这种结构在存储方面比较节约空间,在遍历时的时间复杂度也比用邻接矩阵时更好,两种遍历都为O(V+E)
2.下面以有向图来展示遍历的过程,无向图类似,只是在添加边调用addEdge()
函数时再加一条相反的边即可。
/*A C++ Program to demonstrate adjacency list representation of graphsCreate a adjacency list representation of graphsBFS based on adjacency list representation of graphsBFS based on adjacency list representation of graphs*/#include <iostream>#include <queue>#include <vector>using namespace std;// A structure to represent an adjacency list nodestruct AdjListNode{ int dest; AdjListNode* next; AdjListNode(int d): dest(d), next(NULL) {}};// A structure to represent an adjacency liststruct AdjList{ AdjListNode *head; // pointer to head node of list AdjList(): head(NULL) {}};// A structure to represent a graph. A graph is an array of adjacency lists.// Size of array will be V (number of vertices in graph)struct Graph{ int V; AdjList* array; Graph(int vertex): V(vertex) { array = new AdjList[V]; for(int i = 0; i < vertex; i++) array[i].head = new AdjListNode(i); } ~Graph() { AdjListNode* it; AdjListNode* prev; for(int i = 0; i < V; i++) { it = array[i].head; while(it) { prev = it; it = it->next; delete prev; } } delete[] array; }};// A utility function to create a new adjacency list nodeAdjListNode* newAdjListNode(int dest){ AdjListNode* node = new AdjListNode(dest); return node;}// A utility function that creates a graph of V verticesGraph* createGraph(int V){ Graph* graph = new Graph(V); return graph;}// Adds an edge to an directed graphvoid addEdge(struct Graph* graph, int src, int dest){ //src to dest AdjListNode* node = newAdjListNode(dest); node->next = graph->array[src].head->next; graph->array[src].head->next = node; /* //dest to src (if it's an undirected graph) node = newAdjListNode(src); node->next = graph->array[dest].head; graph->array[dest].head = node; */}// A utility function to print the adjacenncy list representation of graphvoid printGraph(struct Graph* graph){ for (int v = 0; v < graph->V; ++v) { AdjListNode* pCrawl = graph->array[v].head; cout << endl << " Adjacency list of vertex " << v << endl << " "; while (pCrawl) { cout << pCrawl->dest << "-> "; pCrawl = pCrawl->next; } cout << "#" << endl; }}void BreadthFirstSearch(Graph* graph){ cout << "BreadthFirstSearch print: " << endl; queue<AdjListNode*> q; vector<bool> visited(graph->V, false); AdjListNode* cur = NULL; for(int i = 0; i < graph->V; i++) { if(!visited[i]) { visited[i] = true; cout << i << " -> "; cur = graph->array[i].head; q.push(cur); while(!q.empty()) { cur = q.front(); if(cur->dest < graph->V) cur = graph->array[cur->dest].head; q.pop(); while(cur) { if(!visited[cur->dest]) { visited[cur->dest] = true; cout << cur->dest << " -> "; q.push(cur); } cur = cur->next; } } } } cout << "#" << endl;}void DFS_helper(Graph* graph, int v, vector<bool>& visited){ cout << v << " -> "; if(v >= graph->V || v < 0) return; visited[v] = true; AdjListNode* cur = graph->array[v].head->next; while(cur) { DFS_helper(graph, cur->dest, visited); cur = cur->next; }}void DepthFirstSearch(Graph* graph){ cout << "DepthFirstSearch print: " << endl; vector<bool> visited(graph->V, false); for(int i = 0; i < graph->V; i++) { if(!visited[i]) DFS_helper(graph, i, visited); } cout << "#" << endl;}// Driver program to test above functionsint main(){ // create the graph given in above fugure int V = 7; struct Graph* graph = createGraph(V); addEdge(graph, 0, 2); addEdge(graph, 0, 1); addEdge(graph, 1, 4); addEdge(graph, 1, 3); addEdge(graph, 2, 6); addEdge(graph, 2, 5); addEdge(graph, 3, 7); // print the adjacency list representation of the above graph printGraph(graph); //BFS and DFS BreadthFirstSearch(graph); DepthFirstSearch(graph); return 0;}
0 0
- 第7章 图
- 第 7 章 图
- 第1章 第7题
- 第3章 第7题
- 第4章 第7题
- 第7章 第1题
- 第7章第1题
- 第7章第2题
- 第7章第3题
- 第7章第4题
- 第7章 第2题
- 第7章 第3题
- 第7章 第4题
- 第7章 第5题
- 第7章 第6题
- 第7章第5题
- 第7章第6题
- 第10章第7题
- 输出标准体重
- OSX下编译framework bundle
- SQL语句Where中使用别名作为判断条件
- 二叉搜索树的插入和删除
- C++函数重载和const
- 第7章 图
- 360笔试编程题之第一题
- 原码反码和补码
- bits/stat.h:91:21: error: field ‘st_atim’ has incomplete type如何解决
- cocos2d-js 免安装在线版 粒子编辑器 particle editor particle builder 兼容pex和plist
- rtl8188cus编译
- C++ 函数重载和匹配
- shell读文件的方法
- 2016-03-28