Skip to content
Snippets Groups Projects
Commit 1e849a0a authored by François Pitois's avatar François Pitois
Browse files

inital code

parent 0b8dd739
No related branches found
No related tags found
No related merge requests found
Showing
with 340 additions and 0 deletions
run.sh 0 → 100755
#!/bin/bash
cp $1 build/graph.graph
python3 src/part1/unified.py
java -classpath src/part2/GraphCompress/out/production/GraphCompress Main
python3 src/part3/encode.py
# python3 src/part4/draw.py
import networkx as nx
from math import sqrt
def add(l,x, key=lambda x:x):
m = min([key(x) for x in l])
if key(x) <= m:
return
for i in range(len(l)):
if key(l[i]) == m:
l[i] = x
return
def k_max(l,k, key=lambda x:x):
if len(l) <= k:
return l
ans = []
for x in l:
if len(ans) < k:
ans.append(x)
else:
add(ans,x,key)
return ans
def k_struct(H, k, adaptative=False):
G = H.copy()
while G.order() > k:
while G and nx.is_connected(G):
sorted_degree = sorted(G.degree, key = lambda x: x[1], reverse=True)
k_center = sorted_degree[:k]
# k_center = k_max(G.degree, k, key = lambda x: x[1])
center = []
for v, d in k_center:
center.append(v)
G.remove_node(v)
print(">>> removing center (%d nodes), %d remaining" % (k, G.order()))
yield set(center)
if G:
sorted_comp = sorted(list(nx.connected_components(G)), key = len)
bcc = sorted_comp[-1]
spokes = set()
for component in sorted_comp[:-1]:
spokes = spokes.union(component)
yield spokes
# G.remove_nodes_from([n for n in G if n not in set(bcc)])
to_remove = [n for n in G if n in spokes]
G.remove_nodes_from(to_remove)
print(">>> removing spokes (%d nodes), %d remaining" % (len(to_remove), G.order()))
if adaptative:
k = max(10,G.order()//1000)
def k_slashburn(*args, **kwargs):
last = []
for s in k_struct(*args, **kwargs):
last.append(s)
return last
def slashburn(G):
return k_slashburn(G, max(10,G.order()//1000), adaptative=True)
import sys, os
# Disable print
sys.stdout = open(os.devnull, 'w')
import networkx as nx
from cdlib import algorithms, readwrite
from json import loads
from math import sqrt
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import community as community_louvain
import metis
from slashburn import k_slashburn, slashburn
# Restore print
sys.stdout = sys.__stdout__
PATH = "build/"
GRAPH = PATH + "graph.graph"
OUT = PATH + "struct.txt"
def myprint(f,x):
print(x)
f.write(x + '\n')
def list_list_to_string(ll, spec="fc"):
return '\n'.join(map(lambda l: spec + " " + ' '.join(map(str, l)), ll))
def map_list_list(f, ll):
return [list(map(f,l)) for l in ll]
def prune_commu(ll, k):
return [l for l in ll if len(l) >= k]
def partition_to_commu(p):
n = 1+max(p)
commu = [[] for _ in range(n)]
for i, u in enumerate(p):
commu[u].append(i)
return commu
########################
def decorate_cdlib(algo):
def f(G):
return loads(algo(G).to_json())["communities"]
return f
@decorate_cdlib
def big_clam(G):
return algorithms.big_clam(G)
@decorate_cdlib
def louvain(G):
return algorithms.louvain(G)
@decorate_cdlib
def kcut(G):
return algorithms.kcut(G)
def sqrt_metis(G):
k = int(sqrt(G.order()/2))
_, parts = metis.part_graph(G, k)
return partition_to_commu(parts)
def trivial(G):
return [list(range(G.order()))]
def unified():
with open(GRAPH, 'r') as f:
lines = f.readlines()
G = nx.parse_edgelist(lines, nodetype=int, delimiter=",", data=False)
G = nx.convert_node_labels_to_integers(G, label_attribute="old")
threshold = 1
'''
Each function in flist takes a Graph G created with networkx such that V = [0..n-1] for some n,
and returns a list of communities. A community is a list of numbers in [0..n-1]
'''
flist = [
(slashburn, [], "sl"),
(big_clam, [], "bc"),
(louvain, [], "lv"),
(sqrt_metis, [], "mt"),
]
print("Part 1")
i = 0
with open(OUT, 'w') as f:
for algo,args,spec in flist:
commu = algo(G, *args)
pruned = prune_commu(commu, threshold)
reverse = map_list_list(lambda x: G.nodes[x]["old"], pruned)
formated = list_list_to_string(reverse, spec=spec)
f.write(formated + '\n')
i += 1
print("> Algorithm %d/%d done" % (i, len(flist)))
def main():
unified()
main()
# Default ignored files
/shelf/
/workspace.xml
GraphCompress
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/GraphCompress.iml" filepath="$PROJECT_DIR$/GraphCompress.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../../../../.." vcs="Git" />
<mapping directory="$PROJECT_DIR$/../../../../../" vcs="Git" />
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/20.1.0/annotations-20.1.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
\ No newline at end of file
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment