add basic algorithm

This commit is contained in:
Jonas Seiler 2023-06-13 15:08:44 +02:00
commit 3ca94577f9
4 changed files with 243905 additions and 0 deletions

55
Manifest.toml Normal file
View File

@ -0,0 +1,55 @@
# This file is machine-generated - editing it directly is not advised
julia_version = "1.9.0"
manifest_format = "2.0"
project_hash = "5a896dd2d0ff25ca4c1b15e3bcb710a969e917d6"
[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
[[deps.Compat]]
deps = ["UUIDs"]
git-tree-sha1 = "7a60c856b9fa189eb34f5f8a6f6b5529b7942957"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "4.6.1"
[deps.Compat.extensions]
CompatLinearAlgebraExt = "LinearAlgebra"
[deps.Compat.weakdeps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
[[deps.DataStructures]]
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "d1fff3a548102f48987a52a2e0d114fa97d730f0"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.18.13"
[[deps.InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
[[deps.Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
[[deps.OrderedCollections]]
git-tree-sha1 = "d321bf2de576bf25ec4d3e4360faca399afca282"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.6.0"
[[deps.Random]]
deps = ["SHA", "Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
[[deps.SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
version = "0.7.0"
[[deps.Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
[[deps.UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

2
Project.toml Normal file
View File

@ -0,0 +1,2 @@
[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"

243706
dwdswb-headwords.json Normal file

File diff suppressed because it is too large Load Diff

142
main.jl Normal file
View File

@ -0,0 +1,142 @@
using DataStructures
mutable struct node
word::String
neighbours::Vector{Int}
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 longest()
words = readInput()
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)
neighbours = Vector{Int}()
for x in HammingNeighbours(w)
exists, index = binarySearch(x,words)
if exists && index != me
push!(neighbours,index)
end
end
n = node(w,neighbours,false)
push!(graph,n)
end
return graph
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
else
right = 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)
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))
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))
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))
end
return neighbours
end
function BFS(graph::Vector{node})
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
if graph[m].colored == false
currComponent += 1
enqueue!(q,graph[m])
graph[m].colored = true
end
end
end
biggestComponent = max(biggestComponent, currComponent)
if biggestComponent == currComponent
println("Wort: ", graph[i].word)
end
end
end
return components, biggestComponent
end