Compare commits
9 Commits
5d095cff1f
...
cfed832b8b
| Author | SHA1 | Date | |
|---|---|---|---|
| cfed832b8b | |||
| 78c5cd5639 | |||
| 7fd1d68148 | |||
| 05e22b3c79 | |||
| 58bec526d1 | |||
| c4c098fcbd | |||
| 6ca74d2711 | |||
| 859f324b89 | |||
| 5d6762194b |
116
main.jl
116
main.jl
@ -6,47 +6,25 @@ mutable struct node
|
||||
colored::Bool
|
||||
end
|
||||
|
||||
function readInput()
|
||||
words = Vector{String}()
|
||||
for line in eachline("./dwdswb-headwords.json")
|
||||
if line == "{" || line == "}" || line == ""
|
||||
continue
|
||||
end
|
||||
line = strip(line)
|
||||
line = split(line, "\"")
|
||||
if occursin(r"^[a-zA-Z]+$", line[2])
|
||||
push!(words, line[2])
|
||||
end
|
||||
end
|
||||
return sort(words)
|
||||
end
|
||||
|
||||
function readInput2()
|
||||
function readInput(filter=(x->true))
|
||||
words = Vector{String}()
|
||||
for line in eachline("wordlist-german.txt")
|
||||
if !filter(line)
|
||||
continue
|
||||
end
|
||||
push!(words, line)
|
||||
end
|
||||
return sort(words)
|
||||
end
|
||||
|
||||
function longest()
|
||||
words = readInput2()
|
||||
maxLength = maximum(length, words)
|
||||
for w in words
|
||||
if length(w) == maxLength
|
||||
return w
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function createGraph(words::Vector{String})
|
||||
graph = Vector{node}()
|
||||
for w in words
|
||||
_, me = binarySearch(w, words)
|
||||
_, myself = binarySearch(w, words)
|
||||
neighbours = Vector{Int}()
|
||||
for x in HammingNeighbours(w)
|
||||
for x in LevenshteinNeighbours(w)
|
||||
exists, index = binarySearch(x, words)
|
||||
if exists && index != me
|
||||
if exists && index != myself
|
||||
push!(neighbours, index)
|
||||
end
|
||||
end
|
||||
@ -59,74 +37,58 @@ end
|
||||
function binarySearch(word::String, words::Vector{String})
|
||||
left = 1
|
||||
right = length(words)
|
||||
while right - left > 1
|
||||
middle = ceil(Int, (right + left) / 2)
|
||||
if words[middle] == word
|
||||
return true, middle
|
||||
elseif words[middle] < word
|
||||
left = middle
|
||||
while left < right
|
||||
middle = floor(Int, (right + left) / 2)
|
||||
if words[middle] < word
|
||||
left = middle + 1
|
||||
elseif words[middle] > word
|
||||
right = middle - 1
|
||||
else
|
||||
right = middle
|
||||
return true, middle
|
||||
end
|
||||
end
|
||||
middle = (right + left) / 2
|
||||
if words[ceil(Int, middle)] == word
|
||||
return true, ceil(Int, middle)
|
||||
end
|
||||
if words[floor(Int, middle)] == word
|
||||
return true, floor(Int, middle)
|
||||
end
|
||||
return false, 0
|
||||
end
|
||||
|
||||
function HammingNeighbours(InputWord::String)
|
||||
function LevenshteinNeighbours(w::String)
|
||||
neighbours = Vector{String}()
|
||||
word = collect(InputWord)
|
||||
for i in eachindex(word)
|
||||
for c in 'a':'z'
|
||||
wordCopy = copy(word)
|
||||
wordCopy[i] = c
|
||||
push!(neighbours, String(wordCopy))
|
||||
wordCopy = copy(word)
|
||||
wordCopy = insert!(wordCopy, i, c)
|
||||
push!(neighbours, String(wordCopy))
|
||||
w = collect(w)
|
||||
for i in eachindex(w)
|
||||
for c in union('a':'z','A':'Z')
|
||||
word = copy(w)
|
||||
word[i] = c
|
||||
push!(neighbours, String(word))
|
||||
|
||||
word = insert!(copy(w),i,c)
|
||||
push!(neighbours, String(word))
|
||||
end
|
||||
for c in 'A':'Z'
|
||||
wordCopy = copy(word)
|
||||
wordCopy[i] = c
|
||||
push!(neighbours, String(wordCopy))
|
||||
wordCopy = copy(word)
|
||||
wordCopy = insert!(wordCopy, i, c)
|
||||
push!(neighbours, String(wordCopy))
|
||||
end
|
||||
wordCopy = copy(word)
|
||||
wordCopy = deleteat!(wordCopy, i)
|
||||
push!(neighbours, String(wordCopy))
|
||||
word = deleteat!(copy(w), i)
|
||||
push!(neighbours, String(word))
|
||||
end
|
||||
for c in 'a':'z'
|
||||
wordCopy = copy(word)
|
||||
push!(wordCopy, c)
|
||||
push!(neighbours, String(wordCopy))
|
||||
end
|
||||
for c in 'A':'Z'
|
||||
wordCopy = copy(word)
|
||||
push!(wordCopy, c)
|
||||
push!(neighbours, String(wordCopy))
|
||||
|
||||
for c in union('a':'z','A':'Z')
|
||||
word = copy(w)
|
||||
push!(word, c)
|
||||
push!(neighbours, String(word))
|
||||
end
|
||||
return neighbours
|
||||
end
|
||||
|
||||
function BFS(graph::Vector{node})
|
||||
function BFS(g::Vector{node})
|
||||
graph = deepcopy(g)
|
||||
components = 0
|
||||
biggestComponent = 0
|
||||
|
||||
q = Queue{node}()
|
||||
|
||||
for i in eachindex(graph)
|
||||
if graph[i].colored == false
|
||||
|
||||
components += 1
|
||||
currComponent = 1
|
||||
enqueue!(q, graph[i])
|
||||
graph[i].colored = true
|
||||
|
||||
while !isempty(q)
|
||||
n = dequeue!(q)
|
||||
for m in n.neighbours
|
||||
@ -137,14 +99,16 @@ function BFS(graph::Vector{node})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
biggestComponent = max(biggestComponent, currComponent)
|
||||
if biggestComponent == currComponent
|
||||
println("Wort: ", graph[i].word)
|
||||
println("Wort in der größten Komponente: ", graph[i].word)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return components, biggestComponent
|
||||
println("Anzahl Zusammenhangskomponenten: ", components)
|
||||
println("Größe der größten Zusammenhangskomponente: ", biggestComponent)
|
||||
end
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user