Compare commits

..

No commits in common. "cfed832b8b8a1d38f1f916360a9dc7f261059968" and "5d095cff1ff72ba67b75d7f984e96cafe70e5d5a" have entirely different histories.

114
main.jl
View File

@ -6,25 +6,47 @@ mutable struct node
colored::Bool colored::Bool
end end
function readInput(filter=(x->true)) function readInput()
words = Vector{String}() words = Vector{String}()
for line in eachline("wordlist-german.txt") for line in eachline("./dwdswb-headwords.json")
if !filter(line) if line == "{" || line == "}" || line == ""
continue continue
end 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()
words = Vector{String}()
for line in eachline("wordlist-german.txt")
push!(words, line) push!(words, line)
end end
return sort(words) return sort(words)
end 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}) function createGraph(words::Vector{String})
graph = Vector{node}() graph = Vector{node}()
for w in words for w in words
_, myself = binarySearch(w, words) _, me = binarySearch(w, words)
neighbours = Vector{Int}() neighbours = Vector{Int}()
for x in LevenshteinNeighbours(w) for x in HammingNeighbours(w)
exists, index = binarySearch(x, words) exists, index = binarySearch(x, words)
if exists && index != myself if exists && index != me
push!(neighbours, index) push!(neighbours, index)
end end
end end
@ -37,58 +59,74 @@ end
function binarySearch(word::String, words::Vector{String}) function binarySearch(word::String, words::Vector{String})
left = 1 left = 1
right = length(words) right = length(words)
while left < right while right - left > 1
middle = floor(Int, (right + left) / 2) middle = ceil(Int, (right + left) / 2)
if words[middle] < word if words[middle] == word
left = middle + 1
elseif words[middle] > word
right = middle - 1
else
return true, middle return true, middle
elseif words[middle] < word
left = middle
else
right = middle
end end
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 return false, 0
end end
function LevenshteinNeighbours(w::String) function HammingNeighbours(InputWord::String)
neighbours = Vector{String}() neighbours = Vector{String}()
w = collect(w) word = collect(InputWord)
for i in eachindex(w) for i in eachindex(word)
for c in union('a':'z','A':'Z') for c in 'a':'z'
word = copy(w) wordCopy = copy(word)
word[i] = c wordCopy[i] = c
push!(neighbours, String(word)) push!(neighbours, String(wordCopy))
wordCopy = copy(word)
word = insert!(copy(w),i,c) wordCopy = insert!(wordCopy, i, c)
push!(neighbours, String(word)) push!(neighbours, String(wordCopy))
end end
word = deleteat!(copy(w), i) for c in 'A':'Z'
push!(neighbours, String(word)) wordCopy = copy(word)
wordCopy[i] = c
push!(neighbours, String(wordCopy))
wordCopy = copy(word)
wordCopy = insert!(wordCopy, i, c)
push!(neighbours, String(wordCopy))
end end
wordCopy = copy(word)
for c in union('a':'z','A':'Z') wordCopy = deleteat!(wordCopy, i)
word = copy(w) push!(neighbours, String(wordCopy))
push!(word, c) end
push!(neighbours, String(word)) 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))
end end
return neighbours return neighbours
end end
function BFS(g::Vector{node}) function BFS(graph::Vector{node})
graph = deepcopy(g)
components = 0 components = 0
biggestComponent = 0 biggestComponent = 0
q = Queue{node}() q = Queue{node}()
for i in eachindex(graph) for i in eachindex(graph)
if graph[i].colored == false if graph[i].colored == false
components += 1 components += 1
currComponent = 1 currComponent = 1
enqueue!(q, graph[i]) enqueue!(q, graph[i])
graph[i].colored = true graph[i].colored = true
while !isempty(q) while !isempty(q)
n = dequeue!(q) n = dequeue!(q)
for m in n.neighbours for m in n.neighbours
@ -99,16 +137,14 @@ function BFS(g::Vector{node})
end end
end end
end end
biggestComponent = max(biggestComponent, currComponent) biggestComponent = max(biggestComponent, currComponent)
if biggestComponent == currComponent if biggestComponent == currComponent
println("Wort in der größten Komponente: ", graph[i].word) println("Wort: ", graph[i].word)
end end
end end
end end
println("Anzahl Zusammenhangskomponenten: ", components) return components, biggestComponent
println("Größe der größten Zusammenhangskomponente: ", biggestComponent)
end end