Skip to main content

Adjacency list representation of undirected graph in swift

Swift program for Adjacency list representation of undirected graph. Here problem description and explanation.

import Foundation
/*
    Swift 4 Program for 
    Undirected graph representation by using adjacency list 
*/
class AjlistNode
{
	// Vertices node key
	var id: Int;
	var next: AjlistNode? ;
	init(_ id: Int)
	{
		// Set value of node key
		self.id = id;
		self.next = nil;
	}
}
class Vertices
{
	var data: Int;
	var next: AjlistNode? ;
	var last: AjlistNode? ;
	init(_ data: Int)
	{
		self.data = data;
		self.next = nil;
		self.last = nil;
	}
}
class Graph
{
	// Number of Vertices
	var size: Int;
	var node: [Vertices? ];
	init(_ size: Int)
	{
		// Set value
		self.size = size;
		self.node = Array(repeating: nil, count: size);
		self.setData();
	}
	// Set initial node value
	func setData()
	{
		if (self.size <= 0)
		{
			print("\nEmpty Graph");
		}
		else
		{
			var index: Int = 0;
			while (index < self.size)
			{
				self.node[index] = Vertices(index);
				index += 1;
			}
		}
	}
	// Connect two nodes
	func connect(_ start: Int, _ last: Int)
	{
		let edge: AjlistNode? = AjlistNode(last);
		if (self.node[start]!.next == nil)
		{
			self.node[start]!.next = edge;
		}
		else
		{
			// Add edge at the end
			self.node[start]!.last!.next = edge;
		}
		// Get last edge 
		self.node[start]!.last = edge;
	}
	//  Handling the request of adding new edge
	func addEdge(_ start: Int, _ last: Int)
	{
		if (start >= 0 && start < self.size && 
            last >= 0 && last < self.size)
		{
			self.connect(start, last);
			if (start == last)
			{
				// When self loop
				return;
			}
			self.connect(last, start);
		}
		else
		{
			// When invalid nodes
			print("\nHere Something Wrong");
		}
	}
	func printGraph()
	{
		if (self.size > 0)
		{
			do {
				var index: Int = 0;
				// Print graph ajlist Node value
				while (index < self.size)
				{
					print("\nAdjacency list of vertex " + String(index), 
                          terminator: " :");
					var temp: AjlistNode? = self.node[index]!.next;
					while (temp  != nil)
					{
						// Display graph node 
						print("  " + String(self.node[temp!.id]!.data), 
                              terminator: "");
						// Visit to next edge
						temp = temp!.next;
					}
					index += 1;
				}
			}
		}
	}
	static func main(_ args: [String])
	{
		// 5 implies the number of nodes in graph
		let g: Graph? = Graph(5);
		// Connect node with an edge
		g!.addEdge(0, 1);
		g!.addEdge(0, 2);
		g!.addEdge(0, 4);
		g!.addEdge(1, 2);
		g!.addEdge(1, 4);
		g!.addEdge(2, 3);
		g!.addEdge(3, 4);
		// print graph element
		g!.printGraph();
	}
}
Graph.main([String]());

Output

Adjacency list of vertex 0 :  1  2  4
Adjacency list of vertex 1 :  0  2  4
Adjacency list of vertex 2 :  0  1  3
Adjacency list of vertex 3 :  2  4
Adjacency list of vertex 4 :  0  1  3




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment