Created
March 16, 2011 08:44
-
-
Save rik0/872202 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn graph-stats [^Graph g] | |
(vector (clustering-coefficient g) | |
(average-shortest-paths g) | |
(diameter g))) | |
(defn print-table [graph-generator sizes] | |
(println "+----------+------------+------------+------------+") | |
(println "| size | C | APL | D |") | |
(println "+----------+------------+------------+------------+") | |
(doseq [s sizes] | |
(let [g (graph-generator s)] | |
(show-graph g (str "GRAPH" s)) | |
(println | |
(apply format | |
"| % 8d | % 10f | % 10f | % 10f |" | |
s (graph-stats g))) | |
(println "+----------+------------+------------+------------+")))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn edge-factory [] | |
(proxy [Factory] [] | |
(create [] | |
(gensym "E")))) | |
(defn vertex-factory [] | |
(proxy [Factory] [] | |
(create [] | |
(gensym "V")))) | |
(defn make-erdos-renyi [p size] | |
(let [generator | |
(ErdosRenyiGenerator. (UndirectedSparseGraph/getFactory) | |
(vertex-factory) | |
(edge-factory) | |
size | |
p)] | |
(.create generator))) | |
(defn make-barabasi-albert [m starting size] | |
(let [^Collection nodes (range starting) | |
gg (BarabasiAlbertGenerator. | |
(UndirectedSparseGraph/getFactory) | |
(vertex-factory) | |
(edge-factory) starting m (HashSet. nodes))] | |
(.evolveGraph gg (- size starting)) | |
(.create gg))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn show-layout [^Layout layout ^String title] | |
(let [viewer (VisualizationViewer. layout) | |
frame (JFrame. title) | |
pane (JPanel. )] | |
(.add pane viewer) | |
(doto frame | |
(.setContentPane pane) | |
(.pack) | |
(.setVisible true)) | |
frame)) | |
(defmacro show-layout* [layout-class graph-exp title] | |
`(let [^Graph g# ~graph-exp] | |
(show-layout (new ~layout-class g#) (str (quote ~title))))) | |
(defn show-graph | |
([^Graph g] (show-graph g "The Graph")) | |
([^Graph g ^String title] (show-layout (KKLayout. g) title))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment