Added Haskell example for Dijkstras algorithm (#18)
* Adding binary search example for Haskell * Adding selection sort example in Haskell * Adding Haskell examples for chapter 3 * Adding examples for chapter 4 * Adding examples for chapter 5 * Adding git ignore * Add Haskell example for BFS * resetting * Adding haskell example for dijkstras algorithm * Adding Haskell example for chapter 8 * Adding power set based solution for set covering problem * Adding Haskell examples for chap 9
This commit is contained in:
committed by
Aditya Bhargava
parent
1ab56fce62
commit
542f4ab0a0
33
08_greedy_algorithms/Haskell/01_powerset-covering.hs
Normal file
33
08_greedy_algorithms/Haskell/01_powerset-covering.hs
Normal file
@@ -0,0 +1,33 @@
|
||||
import Control.Applicative
|
||||
import Data.List
|
||||
import qualified Data.Set as Set
|
||||
import qualified Data.HashMap.Strict as Map
|
||||
|
||||
stationsMap = Map.fromList [
|
||||
("kone", Set.fromList(["id", "nv", "ut"])),
|
||||
("ktwo", Set.fromList(["wa", "id", "mt"])),
|
||||
("kthree", Set.fromList(["or", "nv", "ca"])),
|
||||
("kfour", Set.fromList(["nv", "ut"])),
|
||||
("kfive", Set.fromList(["ca", "az"]))
|
||||
]
|
||||
|
||||
statesNeeded = Set.fromList ["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]
|
||||
|
||||
powerSet xs = foldl (\acc x -> acc ++ (map (\e -> x:e) acc)) [[]] xs
|
||||
|
||||
allStationCombinations = powerSet $ Map.keys stationsMap
|
||||
|
||||
coverage stationsMap stations = map (`Map.lookup` stationsMap) stations
|
||||
|
||||
stationsCoverage stations =
|
||||
fmap (Set.size . (Set.intersection statesNeeded)) $
|
||||
Just (foldl Set.union Set.empty ) <*>
|
||||
(sequence (coverage stationsMap stations))
|
||||
|
||||
solution = foldl
|
||||
(\x y -> if stationsCoverage x >= stationsCoverage y then x else y)
|
||||
first
|
||||
rest
|
||||
where (first: rest) =
|
||||
sortBy (\a b -> compare (length a) (length b)) $
|
||||
(filter (not . null) allStationCombinations)
|
||||
32
08_greedy_algorithms/Haskell/01_set_convering.hs
Normal file
32
08_greedy_algorithms/Haskell/01_set_convering.hs
Normal file
@@ -0,0 +1,32 @@
|
||||
import qualified Data.Set as Set
|
||||
import qualified Data.HashMap.Strict as Map
|
||||
|
||||
stations = Map.fromList [
|
||||
("kone", Set.fromList(["id", "nv", "ut"])),
|
||||
("ktwo", Set.fromList(["wa", "id", "mt"])),
|
||||
("kthree", Set.fromList(["or", "nv", "ca"])),
|
||||
("kfour", Set.fromList(["nv", "ut"])),
|
||||
("kfive", Set.fromList(["ca", "az"]))
|
||||
]
|
||||
|
||||
statesNeeded = Set.fromList ["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]
|
||||
|
||||
bestStation statesNeeded selectedStations stations = foldl
|
||||
(\a@(station1, states1) b@(station2, states2) ->
|
||||
let fn states = Set.size $ (Set.intersection statesNeeded states)
|
||||
coverage1 = fn states1
|
||||
coverage2 = fn states2
|
||||
in if coverage1 > coverage2 then a else b
|
||||
)
|
||||
x
|
||||
xs
|
||||
where (x: xs) = filter (\(station, states) -> not $ station `elem` selectedStations) $ Map.toList stations
|
||||
|
||||
|
||||
stationSet statesNeeded finalStations =
|
||||
let (station, coveredStations) = bestStation statesNeeded finalStations stations
|
||||
neededStations = Set.difference statesNeeded coveredStations
|
||||
newStations = station : finalStations
|
||||
in if (Set.size statesNeeded > 0) then stationSet neededStations newStations else finalStations
|
||||
|
||||
finalSet = stationSet statesNeeded []
|
||||
Reference in New Issue
Block a user