找出有向图中有没有环

这里用邻接表和一个入度数组实现

topo时候保持一个栈 把入度为0的点压入栈中

每次将栈中元素出栈 同时减去这个点邻接点的入度 

如果临界点的入度为0 则入栈

栈的循环直到栈为空


然后检查是否有环 扫面入度数组 如果有不为0的元素 则存在环


解释可能有不清楚的地方 以后有时间重新写过 睡觉去了!


#include <iostream>
#include <stack>
#include <hash_set>

#define N 26

struct node
{
	int adjVex;
	node* next;
}adj[N];

int inDegree[N];
std::hash_set<int> map;

void init()
{
	memset(inDegree, 0, sizeof(inDegree));
	for(int i = 0; i < N; i++)
	{
		adj[i].adjVex = i;
		adj[i].next = NULL;
	}
};

void create(int u, int v)
{
	if(map.find(u) == map.end())
		map.insert(u);
	if(map.find(v) == map.end())
		map.insert(v);

	node* n = new node();
	n->adjVex = v;
	n->next = adj[u].next;
	adj[u].next = n;
	inDegree[v]++;
};

void printAdjList()
{
	std::cout << "Adj list:\n";
	for(int i = 0; i < N; i++)
	{
		char curElem = adj[i].adjVex + 'A';
		node* n = adj[i].next;

		bool printBreak = false;
		if(n)
		{
			printBreak = true;
			std::cout << curElem << " ";
		}

		while(n)
		{
			char cur = n->adjVex + 'A';
			std::cout << cur << " ";
			n = n->next;
		}
		if(printBreak)
			std::cout << "\n";
	}
};

bool topo()
{
	std::stack<int> st;
	//int cnt = 0;
	for(int i = 0; i < N; i++)
	{
		if(inDegree[i] == 0 && map.find(i) != map.end())
			st.push(i);
	}

	int top;
	std::cout << "Topo result:\n";
	while(!st.empty())
	{
		top = st.top();
		st.pop();
		char cur = top + 'A';
		std::cout << cur << " ";
		//cnt++;
		
		node* n = adj[top].next;
		while(n)
		{
			int k = n->adjVex;
			inDegree[k]--;
			if(inDegree[k] == 0)
				st.push(k);
			n = n->next;
		}
	}
	std::cout << "\n";

	for(int i = 0; i < N; i++)
	{
		if(inDegree[i] != 0)
			return false;
	}

	return true;
};

int main()
{
	int total;
	std::cout << "Put the total of the pairs: ";
	std::cin >> total;

	init();
	char u, v;
	std::cout << "-u- -v-\n";
	while(total--)
	{
		std::cin >> u >> v;
		create(u - 'A', v - 'A');
	}

	printAdjList();

	int canTopo = topo();

	if(!canTopo)
		std::cout << "This graph could not be topoed!\n"; 

	return 0;

}


### 回答1: 下面是一个简单的Julia代码,用于在无向图找出所有: ```julia using LightGraphs function find_cycles(g::AbstractGraph) cycles = [] for node in vertices(g) for neighbor in neighbors(g, node) path = [node, neighbor] current = neighbor while current != node next_node = nextneighbor(g, current, path[end]) if next_node == nothing break end push!(path, next_node) current = next_node end if current == node push!(cycles, path) end end end return cycles end ``` 这段代码使用了LightGraphs包,它是一个用于处理图形数据的高效Julia库。上面的函数接受一个无向图(g)作为输入,并返回一个的列表。首先,它对图的每个节点进行遍历,然后对该节点的每个邻居进行遍历,如果当前节点和开始节点相同,则将路径添加到的列表。 ### 回答2: 在Julia,我们可以用图论库`LightGraphs`来找出无向图的所有。下面是使用`LightGraphs`库的代码示例: ```julia using LightGraphs # 创建一个无向图 graph = Graph(5) # 替换5为你想要的节点数量 # 添加边到图 add_edge!(graph, 1, 2) add_edge!(graph, 1, 3) add_edge!(graph, 2, 3) add_edge!(graph, 3, 4) add_edge!(graph, 4, 5) add_edge!(graph, 5, 1) # 找出无向图的所有 cycles = find_cycle(graph) # 输出所有 for cycle in cycles println(cycle) end ``` 在这个示例,我们创建了一个有5个节点的无向图,然后添加了一些边。接着,我们使用`find_cycle`函数找出了所有的,然后遍历并打印出每个的节点。 注意:这只是一个示例,你可以根据需要修改图的大小、边的连接方式等。 ### 回答3: 在Julia语言,可以使用遍历算法找出无向图的所有。以下是一个示例代码: ```julia # 定义无向图 graph = Dict( 1 => [2, 3, 4], 2 => [1, 4], 3 => [1, 4], 4 => [1, 2, 3, 5], 5 => [4] ) # 定义函数遍历无向图,查所有 function find_cycles(graph, start, node, visited, path, cycles) visited[node] = true push!(path, node) for neighbor in graph[node] if neighbor == start push!(cycles, copy(path)) elseif !visited[neighbor] find_cycles(graph, start, neighbor, visited, path, cycles) end end pop!(path) visited[node] = false end # 主函数,遍历无向图的每个节点 function find_all_cycles(graph) cycles = [] for node in keys(graph) visited = falses(length(graph)) path = [] find_cycles(graph, node, node, visited, path, cycles) end return cycles end # 调用主函数并输出结果 cycles = find_all_cycles(graph) for cycle in cycles println(cycle) end ``` 以上代码首先定义了一个无向图,其`graph`是一个字典数据结构,表示节点之间的连接关系。然后,通过`find_cycles`函数使用深度优先搜索遍历无向图,查所有,并将它们存储在`cycles`数组。最后,通过`find_all_cycles`函数遍历无向图的每个节点,调用`find_cycles`函数查所有,并将结果输出。 请注意,以上代码是一个简单示例,可以根据具体的无向图数据结构进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值