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)
												

最新文章

  1. Java学习笔记 07 接口、继承与多态
  2. 8.11 CSS知识点4
  3. Ubuntu如何安装secureCRT
  4. Android 在布局容器中动态添加控件
  5. 越狱Season 1- Episode 18: Bluff
  6. 深入浅出ghostbuster剖析NodeJS与PhantomJS的通讯机制
  7. HDU 1573 X问题 (中国剩余定理)
  8. java学习之xml
  9. day4作业小代码练习
  10. linkinFrame--用maven搭项目结构
  11. Django 是如何实现用户登录和登出机制的(默认版本-数据库版本)
  12. ClientValidationEnabled
  13. 源码来袭:bind手写实现
  14. web请求流程
  15. Java并发编程(七)-- ThreadLocal
  16. Date对象常用方法
  17. 安装opencv3.x卡在ICV: Downloading ippicv_linux_20151201.tgz...
  18. day 03
  19. UBUNTU16.04 连接不了cn.archive.ubuntu.com
  20. 解决vs2017调试出现脚本错误(/Community/Common7/IDE/PrivateAssemblies/plugin.vs.js) 方法

热门文章

  1. easyui-combobox小Demo
  2. sql 命令操作用法
  3. StandardServiceRegistryBuilder
  4. php读取excel,以及php打包文件夹为zip文件
  5. 开启/关闭ubuntu防火墙
  6. Attributes(1):反射Attribute并输出
  7. VS下面的编译错误-----转换到 COFF 期间失败: 文件无效或损坏
  8. C语言的预处理命令
  9. pageControl设置不居中显示,居左或居右
  10. tyvj 1150 绳子围点 Pick定理 防溢出策略