GraphClient::maxDegree()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 18
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TemplesOfCode\NodesAndEdges;
4
5
/**
6
 * Class GraphClient
7
 * @package TemplesOfCode\NodesAndEdges
8
 */
9
class GraphClient
10
{
11
    /**
12
     * Maximum degree
13
     *
14
     * @param Graph $graph
15
     * @return int
16
     */
17
    public static function maxDegree(Graph $graph)
18
    {
19
        // init
20
        $max = 0;
21
        // get vertices
22
        $vertices = $graph->getVertices();
23
        // iterate over the set of vertices
24
        for ($vertex = 0; $vertex < $vertices; $vertex++) {
25
            // local var
26
            $degree = $graph->degree($vertex);
27
            // check if this vertex degrees are greater than the current max
28
            if ($degree > $max) {
29
                // update the max
30
                $max = $degree;
31
            }
32
        }
33
        // return the max found
34
        return $max;
35
    }
36
37
    /**
38
     * Average degree
39
     *
40
     * @param Graph $graph
41
     * @return int
42
     */
43
    public static function avgDegree(Graph $graph)
44
    {
45
        // each edge incident on two vertices
46
        return 2 * $graph->getEdges() / $graph->getVertices();
47
    }
48
49
    /**
50
     * number of self-loops
51
     *
52
     * @param Graph $graph
53
     * @return int
54
     */
55
    public static function numberOfSelfLoops(Graph $graph)
56
    {
57
        // init
58
        $count = 0;
59
        // get vertices
60
        $vertices = $graph->getVertices();
61
        // iterate over the graph vertices
62
        for ($vertex = 0; $vertex < $vertices; $vertex++) {
63
            // iterate over the adjacent vertices
64
            foreach ($graph->adjacent($vertex) as $w) {
65
                // is this vertex adjacent to itself
66
                if ($vertex == $w) {
67
                    // yes, bump up
68
                    $count++;
69
                }
70
            }
71
        }
72
        // self loop appears in adjacency list twice
73
        return $count/2;   
74
    }
75
}
76