zoukankan      html  css  js  c++  java
  • Graphs and Minimum Cuts(Karger's Min-Cut Algorithm)

    Graphs 

        Two ingredients

         1. vertices (nodes) v

         2. edges(undirected or directed)

    Examples: road networks, the web, social networks


    The minimum Cut problem

        Input: undirected graph G = (V, E)   (parallel edges allowed)

        Goal: compute a cut with fewest number of Crossing edges (a min cut)


    Sparse vs. Dense Graphs

        let n = # of vertices, m = # of edges

        In most applications, m is Omega(n) and O(n^2)

            In a "sparse graph", m is O(n) or close to it

            In a "dense graph",  m is closer to Theta(n^2)


    Two ways to represent a Graph

        1. The Adjacency Matrix 

        2. The Adjacency List

        Which one is better?  Depends on graph density and operation needed.



    Random Contraction Algorithm

        while there are more than 2 vertices:

            -pick a remaining edge(u, v) uniformly at random

            -merge(or "contract") u and v into a single vertex

            -remove self-loops

        return cut represented by final 2 vertices




    Karger's Min-Cut Algorithm -------Random Contraction Algorithm(Python code):

    import random
    import copy
    import time
    
    def contract(ver, e): 
        while len(ver) > 2: #create a new graph every time (not efficient)
            ind = random.randrange(0, len(e))      
            [u, v] = e.pop(ind)   #pick a edge randomly
            ver.remove(v)    #remove v from vertices
            newEdge = list()
            for i in range(len(e)):
                if e[i][0] == v: e[i][0] = u
                elif e[i][1] == v: e[i][1] = u
                if e[i][0] != e[i][1]: newEdge.append(e[i])   # remove self-loops
            e = newEdge
        return(len(e))  #return the number of the remained edges 
    
    if __name__ == '__main__':
        f = open('kargerMinCut.txt')
        _f = list(f) 
        edges = list()          #initialize vertices and edges
        vertices = list()
        for i in range(len(_f)):     #got 2517 different edges
            s = _f[i].split()
            vertices.append(int(s[0]))
            for j in range(1, len(s)):
                if [int(s[j]), int(s[0])] not in edges:
                    edges.append([int(s[0]), int(s[j])])  
    
        result = list()
        starttime = time.clock() 
        for i in range(2000):  #we take n^2logn times so that the Pr(allfail) <= 1/n where n is the number of vertics
            v = copy.deepcopy(vertices)   #notice: deepcopy
            e = copy.deepcopy(edges)
            r = contract(v, e)
            result.append(r)
        endtime = time.clock()
        #print(result)
        print(min(result))
        print(endtime - starttime)


    
    
    



        

  • 相关阅读:
    python面向对象--类的刨析
    python装饰器中高级用法(函数加参)
    运用python中装饰器方法来解决工作中为原有代码添加功能问题
    python学习之路---基础概念扩展:变量,表达式,算法,语句,函数,模块,字符串
    理解cpu过高的逻辑思维与分析方法
    mysql 二进制安装
    K8s高可用集群部署(四)
    K8S集群管理+docker私有仓库harbor搭建(三)
    Python之内置函数的一些使用
    Python之处理svg文件中的style属性
  • 原文地址:https://www.cnblogs.com/riskyer/p/3233977.html
Copyright © 2011-2022 走看看