Skip to main content

Find indegree and outdegree of a directed graph in php

Php program for Find indegree and outdegree of a directed graph. Here more information.

<?php
/*
    Php Program for 
    Show degree of vertex in directed graph
*/
class AjlistNode
{
	// Vertices node key
	public $id;
	public $next;
	public	function __construct($id)
	{
		// Set value of node key
		$this->id = $id;
		$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 $node;
	public	function __construct($size)
	{
		// Set value
		$this->size = $size;
		$this->node = array_fill(0, $size, NULL);
		$this->setData();
	}
	// Set initial node value
	public	function setData()
	{
		if ($this->size <= 0)
		{
			printf("\nEmpty Graph\n");
		}
		else
		{
			for ($index = 0; $index < $this->size; $index++)
			{
				// Set initial node value
				$this->node[$index] = new Vertices($index);
			}
		}
	}
	//  Handling the request of adding new edge
	public	function addEdge($start, $last)
	{
		if ($start >= 0 && $start < $this->size && 
            $last >= 0 && $last < $this->size)
		{
			// Safe connection
			$edge = new AjlistNode($last);
			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;
		}
		else
		{
			// When invalid nodes
			printf("\nHere Something Wrong\n");
		}
	}
	public	function printGraph()
	{
		if ($this->size > 0)
		{
			// Print graph ajlist Node value
			for ($index = 0; $index < $this->size; ++$index)
			{
				printf("\nAdjacency list of vertex %d : ",$index);
				$temp = $this->node[$index]->next;
				while ($temp != NULL)
				{
					// Display graph node 
					printf("%d ",$this->node[$temp->id]->data);
					// Visit to next edge
					$temp = $temp->next;
				}
			}
		}
	}
	// Find indegree and outdegree of 
	// each nodes of a given graph
	public	function findDegree(&$indegree, &$outdegree)
	{
		$temp = NULL;
		$length = 0;
		for ($i = 0; $i < $this->size; ++$i)
		{
			$temp = $this->node[$i]->next;
			$length = 0;
			while ($temp != NULL)
			{
				// Update indegree
				$indegree[$temp->id]++;
				$temp = $temp->next;
				$length++;
			}
			// Set outdegree value
			$outdegree[$i] = $length;
		}
	}
	public	function showDegree()
	{
		if ($this->node != NULL)
		{
			$indegree = array_fill(0, $this->size, 0);
			$outdegree = array_fill(0, $this->size, 0);
			// Set zero degree
			for ($i = 0; $i < $this->size; $i++)
			{
				$indegree[$i] = 0;
				$outdegree[$i] = 0;
			}
			// Find degree
			$this->findDegree($indegree, $outdegree);
			printf("\n\n");
			// Display result
			for ($i = 0; $i < $this->size; ++$i)
			{
				printf("%s\n", "Node [".strval($i).
					"] indegree [".strval($indegree[$i]).
					"] outdegree [".strval($outdegree[$i]).
					"]");
			}
		}
		else
		{
			printf("Empty Graph");
		}
	}
	public static
	function main($args)
	{
		// 6 implies the number of nodes in graph
		$g = new Graph(6);
		// Connected two node with Edges
		$g->addEdge(0, 1);
		$g->addEdge(0, 2);
		$g->addEdge(0, 5);
		$g->addEdge(2, 3);
		$g->addEdge(3, 1);
		$g->addEdge(4, 0);
		$g->addEdge(4, 1);
		$g->addEdge(5, 0);
		$g->addEdge(5, 2);
		$g->printGraph();
		// Test
		$g->showDegree();
	}
}
Graph::main(array());

Output

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

Node [0] indegree [2] outdegree [3]
Node [1] indegree [3] outdegree [0]
Node [2] indegree [2] outdegree [1]
Node [3] indegree [1] outdegree [1]
Node [4] indegree [0] outdegree [2]
Node [5] indegree [1] outdegree [2]




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