图(有向图)的邻接表表示 C++实现(遍历,拓扑排序,最短路径,最小生成树) Implement of digraph and undigraph using adjacency list

本文主要是介绍图(有向图)的邻接表表示 C++实现(遍历,拓扑排序,最短路径,最小生成树) Implement of digraph and undigraph using adjacency list,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文实现了有向图的邻接表表示,并且实现了从创建到销毁图的各种操作。

以及深度优先遍历,广度优先遍历,Dijkstra最短路径算法,Prim最小生成树算法,拓扑排序算法。

可结合我的另一篇文章(有向图,无向图的邻接矩阵表示)看。

PS: 等有时间了作详细的讲解。


#include <iostream>
#include <climits>
#include <sstream> 
#include <queue>
using namespace std;//const bool UNDIGRAPH = 1;struct EdgeNode//edge,the node of linked list  
{  int vtxNO;  int weight;  EdgeNode *next;  
};  struct VNode//vertex, the head of the linked list  
{  string vertexLabel; EdgeNode *first;  bool visited;//only for DFS,BFS,Dijkstraint distance; //only for Dijkstraint path;//only for Dijkstraint indegree; //only for topological sort
};  struct Graph  
{  VNode *vertexList;//the size of this array is equal to vertexes  int vertexes;  int edges;  
};  void BuildGraph(Graph *&graph, int n)
{if (graph == NULL){graph = new Graph;graph->vertexList = new VNode[n];graph->vertexes = n;graph->edges = 0;for (int i = 0; i < n; i++)  {  stringstream ss;  ss<<"v" << i+1;  ss >> graph->vertexList[i].vertexLabel;  graph->vertexList[i].path = -1;graph->vertexList[i].visited = false;graph->vertexList[i].first = NULL;graph->vertexList[i].indegree = 0;}}
}void MakeEmpty(Graph *&graph)
{if(graph == NULL)return;for (int i = 0; i < graph->vertexes; i++)  {  EdgeNode *pEdge = graph->vertexList[i].first;  while (pEdge!=NULL)  {  EdgeNode *tmp = pEdge;  pEdge = pEdge->next;  delete tmp;  }  }  delete graph;
}void AddEdge(Graph *graph,int v1, int v2, int weight)
{if (graph == NULL) return;if (v1 < 0 || v1 > graph->vertexes-1) return;if (v2 < 0 || v2 > graph->vertexes-1) return;if (v1 == v2) return; //no loop is allowedEdgeNode *p = graph->vertexList[v1].first; if(p == NULL)  {  //can not be p = new EdgeNode;  graph->vertexList[v1].first = new EdgeNode;  graph->vertexList[v1].first->next = NULL;  graph->vertexList[v1].first->vtxNO = v2;  graph->vertexList[v1].first->weight = weight;  graph->edges++;graph->vertexList[v2].indegree++;return;}  while (p->next != NULL)//move to the last node  {  if(p->vtxNO == v2)//already exits. checking all nodes but the last one  return;  p = p->next;  }  if(p->vtxNO == v2)//already exits. checking the first or the last node  return;  EdgeNode *node = new EdgeNode;  node->next = NULL;  node->vtxNO = v2;  node->weight = weight;  p->next = node;//last node's next is the new node  graph->edges++;  graph->vertexList[v2].indegree++;
}void RemoveEdge(Graph *graph, int v1, int v2)
{if (graph == NULL) return;if (v1 < 0 || v1 > graph->vertexes-1) return;if (v2 < 0 || v2 > graph->vertexes-1) return;if (v1 == v2) return; //no loop is allowedEdgeNode *p = graph->vertexList[v1].first;  if(p == NULL)//not found  return;  if(p->vtxNO == v2)//found,delete the first node  {  EdgeNode *tmp = p;//first  graph->vertexList[v1].first = p->next; //can not be p = p->next  delete tmp;  graph->edges--;  graph->vertexList[v2].indegree--;return;  }  while(p->next != NULL)  {  if(p->next->vtxNO == v2)//found  {  EdgeNode *tmp = p->next;  p = p->next->next;  delete tmp;  graph->edges--;  graph->vertexList[v2].indegree--;return;  }  p = p->next;  }  
}int GetIndegree(Graph *graph, int v)
{if(graph == NULL) return -1;if(v < 0 || v > graph->vertexes -1) return -2;int degree = 0;  for (int i = 0; i < graph->vertexes; i++)  {  EdgeNode *p = graph->vertexList[i].first;  while (p != NULL)  {  if(p->vtxNO == v)  {  degree++;  break;  }  p = p->next;  }  }  return degree;  
}int GetOutdegree(Graph *graph, int v)
{if(graph == NULL) return -1;if(v < 0 || v > graph->vertexes -1) return -2;int degree = 0;  EdgeNode *p = graph->vertexList[v].first;  while(p != NULL)  {  p = p->next;  degree++;  }  return degree; 
}int GetDegree(Graph *graph, int v)
{if(graph == NULL) return -1;if(v < 0 || v > graph->vertexes -1) return -2;return GetIndegree(graph,v) + GetOutdegree(graph,v);
}void PrintGraph(Graph *graph)
{if(graph == NULL)return;cout << "Vertex: " << graph->vertexes <<"\n";cout << "Edge: " << graph->edges << "\n";for (int i = 0; i < graph->vertexes; i++)  {  cout << i+1 << ": " << graph->vertexList[i].vertexLabel<<"->";  EdgeNode *p = graph->vertexList[i].first;  while (p != NULL)  {  cout << graph->vertexList[p->vtxNO].vertexLabel << "(" << p->weight <<")->";  p = p->next;  }  cout << "\n";  }  cout << "\n";
}//depth first search (use stack or recursion)
//DFS is similar to preorder traversal of trees
void DFS(Graph *graph, int i)
{if (!graph->vertexList[i].visited){cout << graph->vertexList[i].vertexLabel << " ";graph->vertexList[i].visited = true;}EdgeNode *p = graph->vertexList[i].first;while (p != NULL){if(!graph->vertexList[p->vtxNO].visited)//notice!DFS(graph, p->vtxNO);p = p->next;}
}void BeginDFS(Graph *graph)
{if(graph == NULL) return;cout << "DFS\n";for (int i = 0; i < graph->vertexes; i++)graph->vertexList[i].visited = false;for (int i = 0; i < graph->vertexes; i++)DFS(graph,i);
}
//breadth first search(use queue)
//BFS is similar to leverorder traversal of trees
//all of the vertexes will be searched once no matter how the digraph is constructed
void BFS(Graph *graph)
{if(graph == NULL)return;cout << "BFS\n";for (int i = 0; i < graph->vertexes; i++)graph->vertexList[i].visited = false;queue<int> QVertex;for (int i = 0; i < graph->vertexes; i++){if (!graph->vertexList[i].visited){QVertex.push(i);cout << graph->vertexList[i].vertexLabel << " ";graph->vertexList[i].visited = true;}while(!QVertex.empty()){int vtxNO = QVertex.front();QVertex.pop();EdgeNode *p = graph->vertexList[vtxNO].first;while(p != NULL){if (!graph->vertexList[p->vtxNO].visited){cout << graph->vertexList[p->vtxNO].vertexLabel << " ";graph->vertexList[p->vtxNO].visited = true;QVertex.push(p->vtxNO);}p = p->next;}}}cout << "\n";
}void TopologicalSort(Graph *graph)
{//if(UNDIGRAPH) return;if(graph == NULL) return;cout << "TopologicalSort"<<"\n";int counter = 0;queue <int> qVertex;for (int i = 0; i < graph->vertexes; i++){if(GetIndegree(graph,i) == 0)qVertex.push(i);}while (!qVertex.empty()){int vtxNO = qVertex.front();counter++;cout << graph->vertexList[vtxNO].vertexLabel;if(counter != graph->vertexes)cout << " > ";qVertex.pop();EdgeNode *p = graph->vertexList[vtxNO].first;while(p != NULL){int vtxNo = p->vtxNO;/*EdgeNode *tmp = p;p = p->next;delete tmp;tmp = NULL;*/// although tmp is NULL,but p is not NULL,and the data pointed by p has been deletedp = p->next;//if (GetIndegree(graph,vtxNo) == 0)//error,in while(p != NULL),so use a variable indegree insteadif (--graph->vertexList[vtxNo].indegree == 0)qVertex.push(vtxNo);}}cout << "\n";
}void Dijkstra(Graph *graph, int v)
{if(graph == NULL) return;if(v < 0 || v > graph->vertexes-1) return;for (int i = 0; i < graph->vertexes; i++){graph->vertexList[i].visited = false;graph->vertexList[i].distance = INT_MAX;//can delete this line,as initialized in BuildGraphgraph->vertexList[i].path = -1;}graph->vertexList[v].distance = 0;//the rest are all INT_MAXwhile(1){int minDisInx = -1;int minDis = INT_MAX;for (int i = 0; i < graph->vertexes; i++){if(!graph->vertexList[i].visited){if(graph->vertexList[i].distance < minDis){minDis = graph->vertexList[i].distance;minDisInx = i;}}}if(minDisInx == -1)//all visitedbreak;graph->vertexList[minDisInx].visited = true;EdgeNode *p = graph->vertexList[minDisInx].first;while(p != NULL){int vtxNO = p->vtxNO;if(!graph->vertexList[vtxNO].visited){if (graph->vertexList[minDisInx].distance + p->weight < graph->vertexList[vtxNO].distance){graph->vertexList[vtxNO].distance = graph->vertexList[minDisInx].distance + p->weight;graph->vertexList[vtxNO].path = minDisInx;cout << graph->vertexList[vtxNO].vertexLabel << " Updated to " << graph->vertexList[vtxNO].distance << "\n";}}p = p->next;}}cout << "Vertex  Visited  Distance  Path\n";for (int i = 0; i < graph->vertexes; i++){cout << graph->vertexList[i].vertexLabel<< "	";cout << graph->vertexList[i].visited<< "	";cout << graph->vertexList[i].distance<< "	";if(graph->vertexList[i].path == -1)cout << "NONE\n";elsecout << graph->vertexList[graph->vertexList[i].path].vertexLabel << "\n";}
}//almost for undigraph
void Prim(Graph *graph, int v)
{if(graph == NULL) return;if(v < 0 || v > graph->vertexes-1) return;for (int i = 0; i < graph->vertexes; i++){graph->vertexList[i].visited = false;graph->vertexList[i].distance = INT_MAX;//can delete this line,as initialized in BuildGraphgraph->vertexList[i].path = -1;}graph->vertexList[v].distance = 0;//the rest are all INT_MAXwhile(1){int minDisInx = -1;int minDis = INT_MAX;for (int i = 0; i < graph->vertexes; i++){if(!graph->vertexList[i].visited){if(graph->vertexList[i].distance < minDis){minDis = graph->vertexList[i].distance;minDisInx = i;}}}if(minDisInx == -1)//all visitedbreak;graph->vertexList[minDisInx].visited = true;EdgeNode *p = graph->vertexList[minDisInx].first;while(p != NULL){int vtxNO = p->vtxNO;if(!graph->vertexList[vtxNO].visited){if (p->weight < graph->vertexList[vtxNO].distance){graph->vertexList[vtxNO].distance = p->weight;graph->vertexList[vtxNO].path = minDisInx;cout << graph->vertexList[vtxNO].vertexLabel << " Updated to " << graph->vertexList[vtxNO].distance << "\n";}}p = p->next;}}cout << "Vertex  Visited  Distance  Path\n";for (int i = 0; i < graph->vertexes; i++){cout << graph->vertexList[i].vertexLabel<< "	";cout << graph->vertexList[i].visited<< "	";cout << graph->vertexList[i].distance<< "	";if(graph->vertexList[i].path == -1)cout << "NONE\n";elsecout << graph->vertexList[graph->vertexList[i].path].vertexLabel << "\n";}}
int main()
{Graph *graph = NULL;BuildGraph(graph,7);PrintGraph(graph);//for simple test, 0 indexed/*AddEdge(graph,0,1,1);AddEdge(graph,0,2,1);AddEdge(graph,1,3,1);*///for TopologicalSort//0 indexedAddEdge(graph,0,1,1);AddEdge(graph,0,2,1);AddEdge(graph,0,3,1);AddEdge(graph,1,3,1);AddEdge(graph,1,4,1);AddEdge(graph,2,5,1);AddEdge(graph,3,2,1);AddEdge(graph,3,5,1);AddEdge(graph,3,6,1);AddEdge(graph,4,3,1);AddEdge(graph,4,6,1);AddEdge(graph,6,5,1);PrintGraph(graph);RemoveEdge(graph,6,5);AddEdge(graph,6,5,1);//for Dijkstra(shortest path),Prim(minimum spanning tree)//0 indexed/*AddEdge(graph,0,1,2);  AddEdge(graph,0,3,1);  AddEdge(graph,1,3,3);  AddEdge(graph,1,4,10);  AddEdge(graph,2,0,4);  AddEdge(graph,2,5,5);  AddEdge(graph,3,2,2);AddEdge(graph,3,4,2);AddEdge(graph,3,5,8);  AddEdge(graph,3,6,4);  AddEdge(graph,4,6,6);  AddEdge(graph,6,5,1);*/PrintGraph(graph);BeginDFS(graph);cout << "\n";BFS(graph);for (int i = 0; i < graph->vertexes; i++){cout << "\n";Dijkstra(graph,i);}Prim(graph,0);TopologicalSort(graph);MakeEmpty(graph);return 0;
}


这篇关于图(有向图)的邻接表表示 C++实现(遍历,拓扑排序,最短路径,最小生成树) Implement of digraph and undigraph using adjacency list的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/472262

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依