Git Product home page Git Product logo

clustering-coefficient's Introduction

Clustering-Coefficient

Udacity Clustering Coefficient

Code

import UIKit

typealias Segment = (String,String)
typealias NeighborType = Dictionary<String, [String]>

var pairs =  [("ORD", "SEA"), ("ORD", "LAX"), ("ORD", "DFW"), ("ORD", "PIT"), ("SEA", "LAX"), ("LAX", "DFW"), ("ATL", "PIT"), ("ATL", "RDU"), ("RDU", "PHL"), ("PIT", "PHL"), ("PHL", "PVD"), ("DFW", "ATL")]

let neighbors: NeighborType = getNeighbors(from: pairs)

var total: Float = 0.0

neighbors.keys.forEach {
    let ccv = clusteringCoefficient(of: $0, in: neighbors)
    print("\($0) -> \(ccv)")
    total += ccv
}

let average = total / Float(neighbors.keys.count)

print("Total: \(total)")
print("Average: \(average)")



func getNeighbors(from pairs: [Segment]) -> NeighborType {
    var dict = NeighborType()

    for pair in pairs {
        if dict[pair.0] == nil {
            dict[pair.0] = []
        }
        
        if dict[pair.1] == nil {
            dict[pair.1] = []
        }
        
        dict[pair.0]!.append(pair.1)
        dict[pair.1]!.append(pair.0)
    }
    
    return dict
}

func clusteringCoefficient(of node: String, in neighborhood: NeighborType) -> Float {
    
    guard let neighborsOfNode = neighborhood[node] else {
        return 0.0
    }
    
    let kV: Float = Float(neighborsOfNode.count)
    var nV: Float = 0.0
    
    neighborsOfNode.forEach {
        guard let secondDegreeNeighbor = neighborhood[$0] else {
            return
        }
        
        secondDegreeNeighbor
            .filter { $0 != node }
            .forEach {
                
                guard let thirdDegreeNeighbor = neighborhood[$0] else {
                    return
                }
                
                if thirdDegreeNeighbor.contains(node) {
                    nV += 0.5
                }
        }
    }
    
    return kV > 1 ? 2 * nV / (kV * (kV - 1) ) : 0.0
}


Output

PVD -> 0.0
PHL -> 0.0
ATL -> 0.0
RDU -> 0.0
LAX -> 0.666667
ORD -> 0.333333
DFW -> 0.333333
SEA -> 1.0
PIT -> 0.0
Total: 2.33333

Average: 0.259259

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.