Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Symbol extend [ value: arg [^arg perform: self] ]
- Object subclass: Graph [
- | graph |
- Graph class >> new: conxArray [^super new init: conxArray]
- init: conxArray [
- graph := Dictionary new.
- conxArray do: [:str |
- | ends |
- ends := (str substrings: '-') collect: #asSymbol.
- (graph at: ends first ifAbsentPut: [Set new]) add: ends second.
- (graph at: ends second ifAbsentPut: [Set new]) add: ends first.
- ].
- ^self
- ]
- " Part 1: "
- complete3withStartT [
- | res |
- res := Set new.
- " Start at computers beginning with T "
- (graph keys select: [:c | c startsWith: #t]) do: [:comp |
- (graph at: comp) do: [:neigh |
- " Looking in neighbours of neighbours for starting computer "
- ((graph at: neigh) select: [:n | (graph at: n) includes: comp]) do: [:third |
- res add: ({comp. neigh. third} sorted join: ',').
- ].
- ].
- ].
- ^res size
- ]
- " Part 2: Finding maximum sized clique using Bron-Kerbosch algorithm "
- recurseBK: r poss: p exc: x [
- | cliques res poss exc |
- res := r.
- poss := p.
- exc := x.
- (poss isEmpty & exc isEmpty) ifTrue: [^Array with: res].
- cliques := Set new.
- poss do: [:vertex |
- | vertSet |
- vertSet := Set with: vertex.
- cliques addAll: (self recurseBK: (res + vertSet)
- poss: (poss & (graph at: vertex))
- exc: (exc & (graph at: vertex))).
- poss := poss - vertSet.
- exc := exc + vertSet.
- ].
- ^cliques
- ]
- getCliques [
- ^self recurseBK: Set new poss: (Set from: graph keys) exc: Set new.
- ]
- ]
- "
- | Mainline
- "
- graph := Graph new: stdin lines.
- ('Part 1: %1' % {graph complete3withStartT}) displayNl.
- max_clique := graph getCliques fold: [:a :b | (a size > b size) ifTrue: [a] ifFalse: [b]].
- ('Part 2: %1' % {max_clique sorted join: ','}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement