Skip to main content

Minimum weight cycle in an undirected weighted graph in php

Php program for Minimum weight cycle in an undirected weighted graph. Here problem description and explanation.

<?php
/*
    Php program for 
    Minimum weight cycle in an undirected graph
*/
class AjlistNode
{
	// Vertices node key
	public $id;
	public $weight;
	public $next;
	public	function __construct($id, $weight)
	{
		// Set value of node key
		$this->id = $id;
		$this->weight = $weight;
		$this->next = NULL;
	}
}
class Vertices
{
	public $data;
	public $next;
	public $last;
	public	function __construct($data)
	{
		$this->data = $data;
		$this->next = NULL;
		$this->last = NULL;
	}
}
class Graph
{
	// Number of Vertices
	public $size;
	public $result;
	public $node;
	public	function __construct($size)
	{
		// Set value
		$this->size = $size;
		$this->result = 0;
		$this->node = array_fill(0, $size, NULL);
		$this->setData();
	}
	// Set initial node value
	public	function setData()
	{
		if ($this->size <= 0)
		{
			echo "\nEmpty Graph", "\n";
		}
		else
		{
			for ($index = 0; $index < $this->size; $index++)
			{
				// Set initial node value
				$this->node[$index] = new Vertices($index);
			}
		}
	}
	public	function connection($start, $last, $weight)
	{
		// Safe connection
		$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	function addEdge($start, $last, $weight)
	{
		if ($start >= 0 && $start < $this->size && 
            $last >= 0 && $last < $this->size)
		{
			// Connect edge with weight
			$this->connection($start, $last, $weight);
			$this->connection($last, $start, $weight);
		}
		else
		{
			// When invalid nodes
			printf("\nNode missing (%d,%d)", $start, $last);
		}
	}
	public	function printGraph()
	{
		if ($this->size > 0)
		{
			// Print graph ajlist
			for ($index = 0; $index < $this->size; ++$index)
			{
				echo "\nAdjacency list of vertex ",$index,
				" :";
				$edge = $this->node[$index]->next;
				while ($edge != NULL)
				{
					// Display graph node value and weight	
					echo "  ",$this->node[$edge->id]->data,
					"[",$edge->weight,
					"]";
					// Visit to next edge
					$edge = $edge->next;
				}
			}
		}
	}
	public function minimumCycle($start, $last, &$visit, $sum, $length)
	{
		if ($start >= $this->size || 
            $last >= $this->size || 
            $start < 0 || $last < 0 || 
            $this->size <= 0)
		{
			return;
		}
		if ($visit[$start] == true)
		{
			// Here length are indicate loop length
			if ($length > 2 && $start == $last && 
                $sum < $this->result)
			{
				// Here length is indicate number of nodes
				// Because graph is undirected so we consider all cycle 
				// Which contains more than 2 node
				// ---------------------
				// When find a new min weight cycle
				$this->result = $sum;
			}
			return;
		}
		// Here modified  the value of visited node
		$visit[$start] = true;
		// This is used to iterate nodes edges
		$edge = $this->node[$start]->next;
		while ($edge != NULL)
		{
			//  Find solution using recursion
			$this->minimumCycle($edge->id, $last, 
                                $visit, 
                                $sum + ($edge->weight), 
                                $length + 1);
			// Visit to next edge
			$edge = $edge->next;
		}
		// Reset the value of visited node status
		$visit[$start] = false;
	}
	public	function minWeightCycle()
	{
		if ($this->size <= 0)
		{
			// Empty graph
			return;
		}
		// Auxiliary space which is used to store 
		// information about visited node
		// Set initial visited node status 
		$visit = array_fill(0, $this->size, false);
		$this->result = PHP_INT_MAX;
		for ($i = 0; $i < $this->size; ++$i)
		{
			// Check cycle of node i to i
			// Here initial cycle weight is zero
			$this->minimumCycle($i, $i, $visit, 0, 0);
		}
		// Display result
		echo "\nMin weight cycle : ",$this->result, "\n";
	}
	public static
	function main($args)
	{
		// 6 implies the number of nodes in graph
		$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(2, 3, 1);
		$g->addEdge(2, 5, 4);
		$g->addEdge(3, 4, 2);
		$g->addEdge(4, 5, 8);
		$g->addEdge(5, 1, 0);
		// Print graph element
		$g->printGraph();
		// Test
		$g->minWeightCycle();
	}
}
Graph::main(array());

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[4]
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]  2[4]  4[8]  1[0]
Min weight cycle : 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