Dijkstra搜索算法,邻接表搜索(增加了当遇到障碍物时的搜索功能,在第二种里面)

Dijkstra搜索算法,邻接表搜索,两种情况,第一种,直接搜索,搜索结果打印出来;第二种情况,增加了一点其他功能,当遇到要跳过的(或者认为是障碍物的时候)进行搜索。。皆在c++ vs2017下测试可用

// Copyright srcmake.com 2018.
// C++ Example Dijkstra Algorithm For Shortest Path (With PQ/Min-Heap)
// Official webpage for this tutorial: https://www.srcmake.com/home/cpp-shortest-path-dijkstra

/* The Dijkstra algorithm:
	// Initialize the graph adjacency list. adjList[i] = pair<int, int> where first is vertex, second is edge weight.
	// Initialize all source->vertex as infinite.
	// Create a PQ.
	// Add source to pq, where distance is 0.
	// While pq isn't empty...
		// Get min distance vertex from pq. (Call it u.)
		// Visit all of u's friends. For each one (called v)....
			// If the distance to v is shorter by going through u...
				// Update the distance of v.
				// Insert v into the pq.
*/

// The example graph: https://www.srcmake.com/uploads/5/3/9/0/5390645/spgraph_orig.jpg

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

// An adjacency list. Each adjList[i] holds a all the friends of node i.
// The first int is the vertex of the friend, the second int is the edge weight.
vector< vector<pair<int, int> > > FormAdjList()
{
	// Our adjacency list.
	vector< vector<pair<int, int> > > adjList;

	// We have 7 vertices, so initialize 7 rows.
	const int n = 7;

	for (int i = 0; i < n; i++)
	{
		// Create a vector to represent a row, and add it to the adjList.
		vector<pair<int, int> > row;
		adjList.push_back(row);
	}


	// Now let's add our actual edges into the adjacency list.
	// See the picture here: https://www.srcmake.com/uploads/5/3/9/0/5390645/spadjlist_orig.jpg

	adjList[0].push_back(make_pair(1, 2));
	adjList[0].push_back(make_pair(2, 3));

	adjList[1].push_back(make_pair(0, 2));
	adjList[1].push_back(make_pair(5, 1));

	adjList[2].push_back(make_pair(0, 3));
	adjList[2].push_back(make_pair(5, 2));

	adjList[3].push_back(make_pair(1, 4));
	adjList[3].push_back(make_pair(4, 1));
	adjList[3].push_back(make_pair(6, 2));

	adjList[4].push_back(make_pair(3, 1));
	adjList[4].push_back(make_pair(5, 2));
	adjList[4].push_back(make_pair(6, 1));

	adjList[5].push_back(make_pair(1, 1));
	adjList[5].push_back(make_pair(2, 2));
	adjList[5].push_back(make_pair(4, 2));
	adjList[5].push_back(make_pair(6, 2));

	adjList[6].push_back(make_pair(3, 2));
	adjList[6].push_back(make_pair(4, 1));
	adjList[6].push_back(make_pair(5, 2));

	// Our graph is now represented as an adjacency list. Return it.
	return adjList;
}

// Given an Adjacency List, find all shortest paths from "start" to all other vertices.
vector< pair<int, int> > DijkstraSP(vector< vector<pair<int, int> > > &adjList, int &start)
{
	cout << "\nGetting the shortest path from " << start << " to all other nodes.\n";
	vector<pair<int, int> > dist; // First int is dist, second is the previous node. 

	// Initialize all source->vertex as infinite.
	int n = adjList.size();
	for (int i = 0; i < n; i++)
	{
		dist.push_back(make_pair(1000000007, i)); // Define "infinity" as necessary by constraints.
	}

	// Create a PQ.
	priority_queue<pair<int, int>, vector< pair<int, int> >, greater<pair<int, int> > > pq;

	// Add source to pq, where distance is 0.
	pq.push(make_pair(start, 0));
	dist[start] = make_pair(0, start);;

	// While pq isn't empty...
	while (pq.empty() == false)
	{
		// Get min distance vertex from pq. (Call it u.)
		int u = pq.top().first;
		pq.pop();
		/*if (u == 1)
			continue;*/
		// Visit all of u's friends. For each one (called v)....
		for (int i = 0; i < adjList[u].size(); i++)
		{
			int v = adjList[u][i].first;
			int weight = adjList[u][i].second;

			// If the distance to v is shorter by going through u...
			if (dist[v].first > dist[u].first + weight)
			{
				// Update the distance of v.
				dist[v].first = dist[u].first + weight;
				// Update the previous node of v.
				dist[v].second = u;
				// Insert v into the pq. 
				pq.push(make_pair(v, dist[v].first));
			}
		}
	}

	return dist;
}

void PrintShortestPath(vector< pair<int, int> > &dist, int &start)
{
	cout << "\nPrinting the shortest paths for node " << start << ".\n";
	for (int i = 0; i < dist.size(); i++)
	{
		cout << "The distance from node " << start << " to node " << i << " is: " << dist[i].first << endl;

		int currnode = i;
		cout << "The path is: " << currnode;
		while (currnode != start)
		{
			currnode = dist[currnode].second;
			cout << " <- " << currnode;
		}
		cout << endl << endl;
	}
}

int main()
{
	cout << "Program started.\n";

	// Construct the adjacency list that represents our graph. 
	vector< vector<pair<int, int> > > adjList = FormAdjList();

	// Get a list of shortest path distances for node 0.
	int node = 0;
	vector< pair<int, int> > dist = DijkstraSP(adjList, node);

	// Print the list.
	PrintShortestPath(dist, node);

	cout << "Program ended.\n";

	return 0;
}

在这里插入图片描述

// Copyright srcmake.com 2018.
// C++ Example Dijkstra Algorithm For Shortest Path (With PQ/Min-Heap)
// Official webpage for this tutorial: https://www.srcmake.com/home/cpp-shortest-path-dijkstra

/* The Dijkstra algorithm:
	// Initialize the graph adjacency list. adjList[i] = pair<int, int> where first is vertex, second is edge weight.
	// Initialize all source->vertex as infinite.
	// Create a PQ.
	// Add source to pq, where distance is 0.
	// While pq isn't empty...
		// Get min distance vertex from pq. (Call it u.)
		// Visit all of u's friends. For each one (called v)....
			// If the distance to v is shorter by going through u...
				// Update the distance of v.
				// Insert v into the pq.
*/

// The example graph: https://www.srcmake.com/uploads/5/3/9/0/5390645/spgraph_orig.jpg

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

// An adjacency list. Each adjList[i] holds a all the friends of node i.
// The first int is the vertex of the friend, the second int is the edge weight.
vector< vector<pair<int, int> > > FormAdjList()
{
	// Our adjacency list.
	vector< vector<pair<int, int> > > adjList;

	// We have 7 vertices, so initialize 7 rows.
	const int n = 7;

	for (int i = 0; i < n; i++)
	{
		// Create a vector to represent a row, and add it to the adjList.
		vector<pair<int, int> > row;
		adjList.push_back(row);
	}


	// Now let's add our actual edges into the adjacency list.
	// See the picture here: https://www.srcmake.com/uploads/5/3/9/0/5390645/spadjlist_orig.jpg

	adjList[0].push_back(make_pair(1, 2));
	adjList[0].push_back(make_pair(2, 3));

	adjList[1].push_back(make_pair(0, 2));
	adjList[1].push_back(make_pair(5, 1));

	adjList[2].push_back(make_pair(0, 3));
	adjList[2].push_back(make_pair(5, 2));

	adjList[3].push_back(make_pair(1, 4));
	adjList[3].push_back(make_pair(4, 1));
	adjList[3].push_back(make_pair(6, 2));

	adjList[4].push_back(make_pair(3, 1));
	adjList[4].push_back(make_pair(5, 2));
	adjList[4].push_back(make_pair(6, 1));

	adjList[5].push_back(make_pair(1, 1));
	adjList[5].push_back(make_pair(2, 2));
	adjList[5].push_back(make_pair(4, 2));
	adjList[5].push_back(make_pair(6, 2));

	adjList[6].push_back(make_pair(3, 2));
	adjList[6].push_back(make_pair(4, 1));
	adjList[6].push_back(make_pair(5, 2));

	// Our graph is now represented as an adjacency list. Return it.
	return adjList;
}

// Given an Adjacency List, find all shortest paths from "start" to all other vertices.
vector< pair<int, int> > DijkstraSP(vector< vector<pair<int, int> > > &adjList, int &start)
{
	cout << "\nGetting the shortest path from " << start << " to all other nodes.\n";
	vector<pair<int, int> > dist; // First int is dist, second is the previous node. 

	// Initialize all source->vertex as infinite.
	int n = adjList.size();
	for (int i = 0; i < n; i++)
	{
		dist.push_back(make_pair(1000000007, i)); // Define "infinity" as necessary by constraints.
	}

	// Create a PQ.
	priority_queue<pair<int, int>, vector< pair<int, int> >, greater<pair<int, int> > > pq;

	// Add source to pq, where distance is 0.
	pq.push(make_pair(start, 0));
	dist[start] = make_pair(0, start);;

	// While pq isn't empty...
	while (pq.empty() == false)
	{
		// Get min distance vertex from pq. (Call it u.)
		int u = pq.top().first;
		pq.pop();
		if (u == 1)
			continue;//添加了这句话,当遇到不需要搜索的节点时,直接进入下一次循环
		// Visit all of u's friends. For each one (called v)....
		for (int i = 0; i < adjList[u].size(); i++)
		{
			int v = adjList[u][i].first;
			int weight = adjList[u][i].second;

			// If the distance to v is shorter by going through u...
			if (dist[v].first > dist[u].first + weight)
			{
				// Update the distance of v.
				dist[v].first = dist[u].first + weight;
				// Update the previous node of v.
				dist[v].second = u;
				// Insert v into the pq. 
				pq.push(make_pair(v, dist[v].first));
			}
		}
	}

	return dist;
}

void PrintShortestPath(vector< pair<int, int> > &dist, int &start)
{
	cout << "\nPrinting the shortest paths for node " << start << ".\n";
	for (int i = 0; i < dist.size(); i++)
	{
		cout << "The distance from node " << start << " to node " << i << " is: " << dist[i].first << endl;

		int currnode = i;
		cout << "The path is: " << currnode;
		while (currnode != start)
		{
			currnode = dist[currnode].second;
			cout << " <- " << currnode;
		}
		cout << endl << endl;
	}
}

int main()
{
	cout << "Program started.\n";

	// Construct the adjacency list that represents our graph. 
	vector< vector<pair<int, int> > > adjList = FormAdjList();

	// Get a list of shortest path distances for node 0.
	int node = 0;
	vector< pair<int, int> > dist = DijkstraSP(adjList, node);

	// Print the list.
	PrintShortestPath(dist, node);

	cout << "Program ended.\n";

	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dijkstra 算法是一种用于计算图中最短路径的算法,可以用邻接表来实现。下面是一个使用邻接表实现 Dijkstra 算法的示例代码: ```python import heapq def dijkstra(adj, start): # 初始化距离数组 dist = [float('inf')] * len(adj) dist[start] = 0 # 使用堆来存储未处理的节点 heap = [(0, start)] while heap: d, u = heapq.heappop(heap) if d > dist[u]: continue for v, w in adj[u]: if dist[u] + w < dist[v]: dist[v] = dist[u] + w heapq.heappush(heap, (dist[v], v)) return dist ``` 在这个实现中,我们使用了 Python 中的 heapq 库来实现堆。首先,我们初始化一个距离数组,将起点的距离设置为 0,其余节点的距离设置为无穷大。然后,我们将起点加入堆中。接下来,我们不断地从堆中取出距离最小的节点,并更新与之相邻的节点的距离。如果更新后的距离比原来的距离更小,我们就将它加入堆中。最后,我们返回距离数组。 在邻接表中,每个节点对应一个列表,其中包含该节点连向的所有节点及其权重。在示例代码中,我们将邻接表存储为一个列表,其中每个元素是一个包含节点和其邻居的列表。例如,如果节点 0 连向节点 1 和节点 2,权重分别为 2 和 5,则邻接表中的第一个元素为 [(1, 2), (2, 5)]。 注意,由于 Dijkstra 算法中使用了堆来实现优先队列,因此间复杂度为 O((E+V)logV),其中 E 表示边数,V 表示节点数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值