Skip to content

Instantly share code, notes, and snippets.

@evgenii-malov
evgenii-malov / avl.hs
Last active February 2, 2022 14:47
AVL tree with Haskell
-- GHCi, version 8.8.4
-- author: Evgeniy Malov
-- AVL delete: https://www.youtube.com/watch?v=DfSeb2fDH3s
-- AVL insert: https://www.youtube.com/watch?v=SlAJirZ0KTE&t=0s
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Monad
import Data.Maybe
import qualified Data.List as L
import qualified Data.Map as M
import PrettyT -- https://gist.github.com/evgenii-malov/1fc29a652751451dbca0d54d454cc1ef
@evgenii-malov
evgenii-malov / ds_cycle_cc.hs
Created January 27, 2022 08:50
Find cycle and connected components with disjoint set with haskell
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Monad
import Data.Maybe
import qualified Data.List as L
import qualified Data.Map as M
data Uedge a = Ue (a,a) deriving Show
instance Eq a => Eq (Uedge a) where
(==) (Ue (a,b)) (Ue (a1,b1)) = (a == a1 && b==b1 ) || (a==b1 && b==a1)
@evgenii-malov
evgenii-malov / prim.hs
Last active January 23, 2022 11:19
Prim's algorithm in Haskell
-- video https://studio.youtube.com/video/KJkpvt2787g/edit
{-# LANGUAGE ScopedTypeVariables #-}
--{-# LANGUAGE AllowAmbiguousTypes #-}
import Control.Monad
import Data.List
import Data.Maybe
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Bheap as H -- https://gist.github.com/evgenii-malov/78e690007a60b2230676bc3f1ee50052
@evgenii-malov
evgenii-malov / diekstra_bheap.hs
Last active January 14, 2022 23:03
diekstra with binary heap
-- https://www.youtube.com/watch?v=NRzjkrrDJyQ
{-# LANGUAGE ScopedTypeVariables #-}
-- GHCi, version 8.8.4
import qualified Bheap as H -- https://gist.github.com/evgenii-malov/78e690007a60b2230676bc3f1ee50052
import Control.Monad
import Data.List
import Data.Maybe
import qualified Data.Map as M
-- Dijkstra's algorithm solves the shortest-path problem for any weighted, directed graph
@evgenii-malov
evgenii-malov / min_heap.hs
Last active January 16, 2022 16:58
min binary heap
-- Whatch videos about binary heap
-- https://www.youtube.com/watch?v=2uWbUd-sEgM
-- https://www.youtube.com/watch?v=hSVC1fHzGQ0
-- https://www.youtube.com/watch?v=bfrXhzRNL2g
-- https://www.youtube.com/watch?v=8oRm3jCpBoE
{-# LANGUAGE FlexibleInstances #-}
--{-# LANGUAGE AllowAmbiguousTypes #-} -- ??
module Bheap where
import PrettyT -- https://gist.github.com/evgenii-malov/1fc29a652751451dbca0d54d454cc1ef
@evgenii-malov
evgenii-malov / bheap.hs
Created January 13, 2022 17:25
Binary heap (with typeclass abstraction)
{-# LANGUAGE FlexibleInstances #-}
--{-# LANGUAGE AllowAmbiguousTypes #-} -- ??
import PrettyT
import Data.List
import qualified Data.Map as M
import Data.Maybe
import Control.Monad
import Control.Applicative
-- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show
@evgenii-malov
evgenii-malov / d.hs
Last active January 9, 2022 20:36
Dijkstra's algorithm in haskell
{-# LANGUAGE ScopedTypeVariables #-}
-- GHCi, version 8.8.4
-- explanation video https://www.youtube.com/watch?v=gcjL7BlrsTM
import Control.Monad
import Data.List
import Data.Maybe
import qualified Data.Map as M
-- Dijkstra's algorithm solves the shortest-path problem for any weighted, directed graph
-- with non-negative weights. It can handle graphs consisting of cycles,
@evgenii-malov
evgenii-malov / binary_heap.hs
Last active January 7, 2022 08:06
binary heap insertion, extract root and heapify with haskell
import PrettyT -- https://www.youtube.com/watch?v=Ud-1Z0hBlB8&t=15s
import Data.List
import Data.Maybe
import Control.Monad
import Control.Applicative
-- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show
--In a complete binary tree every level, except possibly the last, is completely filled,
-- and all nodes in the last level are as far left as possible.
-- It can have between 1 and 2h nodes at the last level h
@evgenii-malov
evgenii-malov / tree_as_list.hs
Last active January 3, 2022 22:34
Represent tree as list in haskell
-- see video - https://www.youtube.com/watch?v=PWL68hPTwxQ
import PrettyT
import Data.List
--In a complete binary tree every level, except possibly the last, is completely filled,
-- and all nodes in the last level are as far left as possible.
-- It can have between 1 and 2h nodes at the last level h
ctb :: Int -> [a] -> Btree a
ctb _ [] = Empty
@evgenii-malov
evgenii-malov / graphs_example.hs
Last active June 20, 2022 09:57
basic functions to work with graphs in haskell
-- see videos https://www.youtube.com/watch?v=UM0sggwLXk4&t=974s
-- https://www.youtube.com/watch?v=RS7eIkETdIQ
-- https://www.youtube.com/watch?v=UM0sggwLXk4&t=974s
import Control.Monad
import Data.List
import Data.Maybe
data Uedge a = Ue (a,a) deriving Show
(<->) a b = Ue (a,b)