Блог пользователя ameyaosd

Автор ameyaosd, 4 года назад, По-английски

Does any one have a (hopefully short) neat implementation of graph representation in Golang?

Here is my dfs implementation in C++. I'm looking for a equivalent in golang

const int msz=1000;
vector<int> g[msz];
int level[msz];
bool visited[msz];

void dfs(int s)
{
	visited[s]=true;
	for(int i=0;i<g[s].size();i++)
	{
		if(visited[g[s][i]]==false)
		{
			level[g[s][i]]=level[s]+1;
			dfs(g[s][i]);
		}
	}
}

int main()
{
	fio;
	int nodes,edges;
	cin>>nodes>>edges;
	for(int i=0;i<edges;i++)
	{
		int x,y;
		cin>>x>>y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	int source;
	cin>>source;
	dfs(source);
	return 0;
}
  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
This should work (not tested)