Posted on by Kalkicode
Code Graph

Find indegree and outdegree of a directed graph in golang

Go program for Find indegree and outdegree of a directed graph. Here problem description and other solutions.

package main
import "fmt"
/*
    Go Program for 
    Show degree of vertex in directed graph
*/
type AjlistNode struct {
    // Vertices node key
    id int
    next * AjlistNode
}
func getAjlistNode(id int) * AjlistNode {
    var me *AjlistNode = &AjlistNode {}
    // Set value of node key
    me.id = id
    me.next = nil
    return me
}
type Vertices struct {
    data int
    next * AjlistNode
    last * AjlistNode
}
func getVertices(data int) * Vertices {
    var me *Vertices = &Vertices {}
    me.data = data
    me.next = nil
    me.last = nil
    return me
}
type Graph struct {
    // Number of Vertices
    size int
    node [] *Vertices
}
func getGraph(size int) * Graph {
    var me *Graph = &Graph {size,make([] *Vertices, size)}
    // Set value
    me.setData()
    return me
}
// Set initial node value
func(this *Graph) setData() {
    if this.size <= 0 {
        fmt.Println("\nEmpty Graph")
    } else {
        for index := 0 ; index < this.size ; index++ {
            // Set initial node value
            this.node[index] = getVertices(index)
        }
    }
}
//  Handling the request of adding new edge
func(this *Graph) addEdge(start, last int) {
    if start >= 0 && start < this.size && 
        last >= 0 && last < this.size {
        // Safe connection
        var edge * AjlistNode = getAjlistNode(last)
        if this.node[start].next == nil {
            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
        fmt.Println("\nHere Something Wrong")
    }
}
func(this Graph) printGraph() {
    if this.size > 0 {
        // Print graph ajlist Node value
        for index := 0 ; index < this.size ; index++ {
            fmt.Print("\nAdjacency list of vertex ", index, " : ")
            var temp * AjlistNode = this.node[index].next
            for (temp != nil) {
                // Display graph node 
                fmt.Print(" ", this.node[temp.id].data)
                // Visit to next edge
                temp = temp.next
            }
        }
    }
}
// Find indegree and outdegree of 
// each nodes of a given graph
func(this Graph) findDegree(indegree []int, outdegree []int) {
    var temp * AjlistNode = nil
    var length int = 0
    for i := 0 ; i < this.size ; i++ {
        temp = this.node[i].next
        length = 0
        for (temp != nil) {
            // Update indegree
            indegree[temp.id]++
            temp = temp.next
            length++
        }
        // Set outdegree value
        outdegree[i] = length
    }
}
func(this Graph) showDegree() {
    if this.node != nil {
        var indegree = make([] int, this.size)
        var outdegree = make([] int, this.size)
     
        // Find degree
        this.findDegree(indegree, outdegree)
        fmt.Println("\n")
        // Display result
        for i := 0 ; i < this.size ; i++ {
            fmt.Println("Node [", i, "] indegree [", indegree[i], "] outdegree [", outdegree[i], "]")
        }
    } else {
        fmt.Print("Empty Graph")
    }
}
func main() {
    // 6 implies the number of nodes in graph
    var g * Graph = getGraph(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()
}

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