Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
1.7 kB
3
Indexable
Never
using System;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
		List<Node> Vertices = new List<Node>();
		Node v1 = Functions.CreateVertex(1,null);
		Node v2 = Functions.CreateVertex(2,null);
		Node v3 = Functions.CreateVertex(3,null);
		Node v4 = Functions.CreateVertex(4,null);
		Node v5 = Functions.CreateVertex(5,null);
		Node v6 = Functions.CreateVertex(6,null);
		Node v7 = Functions.CreateVertex(7,null);
		Node v8 = Functions.CreateVertex(8,null);
		
		v1.Children = new List<Node>(){v2,v3};
		v2.Children = new List<Node>(){v4,v5,v6};
		v4.Children = new List<Node>(){v7,v8};
		
		Vertices.Add(v1);
		Vertices.Add(v2);
		Vertices.Add(v3);
		Vertices.Add(v4);
		Vertices.Add(v5);
		
		Functions.IterateGraph(Vertices);
	}
}
public class Node
{
	
	public int Data;
	public bool Iterated;
	public List<Node> Children = new List<Node>();
	public Node(int Data,List<Node> Children = null)
	{
		this.Data = Data;
		this.Children = Children;
	}
	public void Print()
	{
		Console.WriteLine(Data);
	}
}
public static class Functions
{
	public static Node CreateVertex(int Data, List<Node> Children)
	{
		Node node = new Node(Data,Children);
		return node;
	}
	public static void IterateGraph(List<Node> Vertices)
	{
		for(int i=0;i<Vertices.Count;i++)
		{
			if(Vertices[i].Iterated == true)
				continue;
			Vertices[i].Iterated = true;
			
			Vertices[i].Print();
			if(Vertices[i].Children != null)
			{
				for(int k = 0;k<Vertices[i].Children.Count;k++)
				{
					Vertices[i].Children[k].Iterated = true;
					Vertices[i].Children[k].Print();
				}
			}
		}
	}
}
Leave a Comment