Skip to main content

Find maximum weight cycle in an undirected graph in typescript

Ts program for Find maximum weight cycle in an undirected graph. Here more information.

/*
    TypeScript program for 
    Maximum weight cycle in an undirected graph
*/
class AjlistNode
{
	// Vertices node key
	public id: number;
	public weight: number;
	public next: AjlistNode;
	constructor(id: number, weight: number)
	{
		// Set value of node key
		this.id = id;
		this.weight = weight;
		this.next = null;
	}
}
class Vertices
{
	public data: number;
	public next: AjlistNode;
	public last: AjlistNode;
	constructor(data: number)
	{
		this.data = data;
		this.next = null;
		this.last = null;
	}
}
class Graph
{
	// Number of Vertices
	public size: number;
	public result: number;
	public node: Vertices[];
	constructor(size: number)
	{
		// Set value
		this.size = size;
		this.result = 0;
		this.node = Array(size).fill(null);
		this.setData();
	}
	// Set initial node value
	public setData()
	{
		if (this.size <= 0)
		{
			console.log("\nEmpty Graph");
		}
		else
		{
			for (var index = 0; index < this.size; index++)
			{
				// Set initial node value
				this.node[index] = new Vertices(index);
			}
		}
	}
	public connection(start: number, last: number, weight: number)
	{
		// Safe connection
		var edge = new AjlistNode(last, weight);
		if (this.node[start].next == null)
		{
			this.node[start].next = edge;
		}
		else
		{
			// Add edge at the end
			this.node[start].last.next = edge;
		}
		// Get last edge 
		this.node[start].last = edge;
	}
	//  Handling the request of adding new edge
	public addEdge(start: number, last: number, weight: number)
	{
		if (start >= 0 && start < this.size && last >= 0 && last < this.size)
		{
			this.connection(start, last, weight);
			if (start == last)
			{
				return;
			}
			this.connection(last, start, weight);
		}
		else
		{
			// When invalid nodes
			console.log("\nHere Something Wrong");
		}
	}
	public printGraph()
	{
		if (this.size > 0)
		{
			// Print graph ajlist Node value
			for (var index = 0; index < this.size; ++index)
			{
				console.log("\nAdjacency list of vertex " + index + " :");
				var edge = this.node[index].next;
				while (edge != null)
				{
					// Display graph node 
					console.log("  " + this.node[edge.id].data + "[" + edge.weight + "]");
					// Visit to next edge
					edge = edge.next;
				}
			}
		}
	}
	public maximumCycle(start: number, last: number, visit: boolean[], sum: number)
	{
		if (start >= this.size || last >= this.size || start < 0 || last < 0 || this.size <= 0)
		{
			return;
		}
		if (visit[start] == true)
		{
			if (start == last && sum > this.result)
			{
				// When find a new max weight cycle
				this.result = sum;
			}
			return;
		}
		// Here modified  the value of visited node
		visit[start] = true;
		// This is used to iterate nodes edges
		var edge = this.node[start].next;
		while (edge != null)
		{
			this.maximumCycle(edge.id, last, visit, sum + (edge.weight));
			// Visit to next edge
			edge = edge.next;
		}
		// Reset the value of visited node status
		visit[start] = false;
	}
	public maxWeightCycle()
	{
		if (this.size <= 0)
		{
			// Empty graph
			return;
		}
		// Auxiliary space which is used to store 
		// information about visited node
		var visit: boolean[] = Array(this.size).fill(false);
		this.result = -Number.MAX_VALUE;
		for (var i = 0; i < this.size; ++i)
		{
			// Check cycle of node i to i
			// Here initial cycle weight is zero
			this.maximumCycle(i, i, visit, 0);
		}
		if (this.result == -Number.MAX_VALUE)
		{
			console.log("\nMax weight cycle : None ");
		}
		else
		{
			console.log("\nMax weight cycle : " + this.result);
		}
	}
	public static main(args: string[])
	{
		// 6 implies the number of nodes in graph
		var g = new Graph(6);
		// Connect node with an edge
		// First and second parameter indicate node
		// Last parameter is indicate weight
		g.addEdge(0, 1, 3);
		g.addEdge(0, 3, -3);
		g.addEdge(0, 4, 7);
		g.addEdge(0, 5, 1);
		g.addEdge(1, 2, 11);
		g.addEdge(1, 4, 8);
		g.addEdge(1, 5, 0);
		g.addEdge(2, 3, 1);
		g.addEdge(2, 5, 5);
		g.addEdge(3, 4, 2);
		g.addEdge(4, 5, 8);
		// Print graph element
		g.printGraph();
		// Test
		g.maxWeightCycle();
	}
}
Graph.main([]);
/*
 file : code.ts
 tsc --target es6 code.ts
 node code.js
 */

Output

Adjacency list of vertex 0 :
  1[3]
  3[-3]
  4[7]
  5[1]

Adjacency list of vertex 1 :
  0[3]
  2[11]
  4[8]
  5[0]

Adjacency list of vertex 2 :
  1[11]
  3[1]
  5[5]

Adjacency list of vertex 3 :
  0[-3]
  2[1]
  4[2]

Adjacency list of vertex 4 :
  0[7]
  1[8]
  3[2]
  5[8]

Adjacency list of vertex 5 :
  0[1]
  1[0]
  2[5]
  4[8]

Max weight cycle : 34




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