Loading
  • 21 Aug, 2019

  • By, Wikipedia

File:Critical 1000-vertex Erdős–Rényi–Gilbert Graph.svg

Source code

from PADS.SVG import *
from PADS.StrongConnectivity import *
from random import random
from sys import stdout

# ===================================================
# Generate a random graph and random layout
# ===================================================

n = 1000
vertices = range(n)
edgeprob = 1./(n-1)

halfG = {v : set(w for w in vertices if v<w and random() < edgeprob) for v in vertices}
G = {v : set(w for w in vertices if v in halfG[w] or w in halfG[v]) for v in vertices}

# ===================================================
# Pull giant component in and push all the rest out
# ===================================================
weight = {}
SCC = StronglyConnectedComponents(G)
giant = max(len(C) for C in SCC)
for C in StronglyConnectedComponents(G):
    for v in C:
        if len(C) == giant:
            weight[v] = giant
        else:
            weight[v] = -1

# ===================================================
# Social gravity
# ===================================================

D = {v : (random()-0.5) + 1j* (random()-0.5) for v in vertices}
natlength = n**(-0.5)
iterations = 150
increment = 0.01

for i in range(iterations):
    social = 0.25
    forces = {v : -D[v]*social for v in vertices}

    for v in vertices:
        for w in vertices:
            if v != w:
                forces[v] += (natlength/abs(D[v]-D[w]))**2*(D[v]-D[w])

    for v in vertices:
        for w in G[v]:
            forces[v] += abs(D[v]-D[w])*(D[w]-D[v])/natlength

    for v in vertices:
        D[v] += increment * forces[v]

# ===================================================
# Renormalize
# ===================================================

minx = min(D[v].real for v in vertices)
miny = min(D[v].imag for v in vertices)
offset = minx + 1j*miny
for v in vertices:
    D[v] -= offset

maxx = max(D[v].real for v in vertices)
maxy = max(D[v].imag for v in vertices)
rescale = 1./max(maxx,maxy)
for v in vertices:
    D[v] *= rescale

# ===================================================
# Turn layout into drawing
# ===================================================

scale = 1000
radius = 6
margin = 9
bbox = scale*(1+1j)

def place(v):
    return D[v]*(scale-2*margin) + margin*(1+1j)

drawing = SVG(bbox,stdout)

drawing.group(style={"stroke":"#000","stroke-width":"2"})
for v in vertices:
    for w in halfG[v]:
        drawing.segment(place(v),place(w))
drawing.ungroup()

drawing.group(fill=colors.red,stroke=colors.black)
for v in vertices:
    drawing.circle(place(v),radius)
drawing.ungroup()

drawing.close()

Captions

An Erdős–Rényi–Gilbert graph with 1000 vertices at the critical edge probability

Items portrayed in this file

depicts

8 February 2022

image/svg+xml

2bab09c61e6c052133f6acc2c459b2c2dc18eedf

80,529 byte

1,000 pixel

1,000 pixel

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current07:33, 9 February 2022Thumbnail for version as of 07:33, 9 February 20221,000 × 1,000 (79 KB)David EppsteinUploaded own work with UploadWizard

Metadata