Added chapter 6

This commit is contained in:
Nishant Shukla 2014-06-21 11:31:36 -04:00
parent 2680bc1f26
commit aa1aa43d3b
32 changed files with 509 additions and 1 deletions

View File

@ -0,0 +1,25 @@
-- Initial Code0501.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: Code0601
version: 0.1.0.0
synopsis: Graph form list of edges.
-- description:
homepage: haskelldata.com
license: GPL-2
license-file: ../LICENSE
author: Nishant Shukla
maintainer: nick722@gmail.com
-- copyright:
category: Data
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable Code0601
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.6 && <4.7, containers >=0.5 && <0.6
-- hs-source-dirs:
default-language: Haskell2010

View File

@ -0,0 +1,13 @@
import Data.Graph
-- Defining a graphical network
myGraph :: Graph
myGraph = buildG bounds edges
where bounds = (1,4)
edges = [ (1,3), (1,4)
, (2,3), (2,4)
, (3,4) ]
main = do
putStrLn $ "The edges are " ++ (show.edges) myGraph
putStrLn $ "The vertices are " ++ (show.vertices) myGraph

View File

@ -0,0 +1,5 @@
default:
cabal install --bindir=./
clean:
rm -rf dist Setup.hs Code0601 *~

View File

@ -0,0 +1,25 @@
-- Initial Code0502.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: Code0602
version: 0.1.0.0
synopsis: Graph from adjacency list
-- description:
homepage: haskelldata.com
license: GPL-2
license-file: ../LICENSE
author: Nishant Shukla
maintainer: nick722@gmail.com
-- copyright:
category: Data
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable Code0602
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.6 && <4.7, containers >=0.5 && <0.6
-- hs-source-dirs:
default-language: Haskell2010

View File

@ -0,0 +1,12 @@
import Data.Graph
-- Defining a graph edges
myGraph :: Graph
myGraph = fst $ graphFromEdges' [ ("Node 1", 1, [3, 4])
, ("Node 2", 2, [3, 4])
, ("Node 3", 3, [4])
, ("Node 4" ,4, []) ]
main = do
putStrLn $ "The edges are " ++ (show.edges) myGraph
putStrLn $ "The vertices are " ++ (show.vertices) myGraph

View File

@ -0,0 +1,5 @@
default:
cabal install --bindir=./
clean:
rm -rf dist Setup.hs Code0602 *~

View File

@ -0,0 +1,25 @@
-- Initial Code0503.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: Code0603
version: 0.1.0.0
synopsis: Topological sort of a graph.
-- description:
homepage: haskelldata.com
license: GPL-2
license-file: ../LICENSE
author: Nishant Shukla
maintainer: nick722@gmail.com
-- copyright:
category: Data
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable Code0603
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.6 && <4.7, containers >=0.5 && <0.6
-- hs-source-dirs:
default-language: Haskell2010

20
Ch06/Code03_topo/Main.hs Normal file
View File

@ -0,0 +1,20 @@
import Data.Graph
import Data.Map (Map, (!), fromList)
import Data.List (nub)
-- Topologically sort a graph
main = do
ls <- fmap lines getContents
let g = graph ls
putStrLn $ showTopoSort ls g
graph ls = buildG bounds edges
where bounds = (1, (length.nub) ls)
edges = tuples $ map (mappingStrToNum !) ls
mappingStrToNum = fromList $ zip (nub ls) [1..]
tuples (a:b:cs) = (a, b) : tuples cs
tuples _ = []
showTopoSort ls g =
unlines $ map (mappingNumToStr !) (topSort g)
where mappingNumToStr = fromList $ zip [1..] (nub ls)

View File

@ -0,0 +1,5 @@
default:
cabal install --bindir=./
clean:
rm -rf dist Setup.hs Code0603 *~

6
Ch06/Code03_topo/input.txt Executable file
View File

@ -0,0 +1,6 @@
understand Haskell
do Haskell data analysis
understand data analysis
do Haskell data analysis
do Haskell data analysis
find patterns in big data

View File

@ -0,0 +1,25 @@
-- Initial Code0504.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: Code0604
version: 0.1.0.0
synopsis: Traversing a graph depth first.
-- description:
homepage: haskelldata.com
license: GPL-2
license-file: ../LICENSE
author: Nishant Shukla
maintainer: nick722@gmail.com
-- copyright:
category: Data
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable Code0604
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.6 && <4.7, containers >=0.5 && <0.6, array >=0.4 && <0.5
-- hs-source-dirs:
default-language: Haskell2010

19
Ch06/Code04_depth/Main.hs Normal file
View File

@ -0,0 +1,19 @@
import Data.Graph
import Data.Array ((!))
graph :: (Graph, Vertex -> (Int, Int, [Int]))
graph = graphFromEdges' [ (1, 1, [3, 4] )
, (2, 2, [3, 4])
, (3, 3, [4])
, (4, 4, []) ]
-- Traversing a graph depth-first
main = do
print $ depth graph 0
depth g i = depth' g [] i
depth' g@(gShape, gMapping) seen i =
key : concat (map goDeeper adjacent)
where goDeeper v = if v `elem` seen then [] else depth' g (i:seen) v
adjacent = gShape ! i
(_, key, _) = gMapping i

View File

@ -0,0 +1,5 @@
default:
cabal install --bindir=./
clean:
rm -rf dist Setup.hs Code0604 *~

View File

@ -0,0 +1,25 @@
-- Initial Code0505.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: Code0605
version: 0.1.0.0
synopsis: Traversing a graph breadth first.
-- description:
homepage: haskelldata.com
license: GPL-2
license-file: ../LICENSE
author: Nishant Shukla
maintainer: nick722@gmail.com
-- copyright:
category: Data
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable Code0605
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.6 && <4.7, containers >=0.5 && <0.6, array >=0.4 && <0.5
-- hs-source-dirs:
default-language: Haskell2010

View File

@ -0,0 +1,20 @@
import Data.Graph
import Data.List (tails)
import Data.Array ((!))
-- Traversing a graph breadth-first
main = do
print $ breadth graph 1
breadth g i = bf [] [i]
where bf :: [Int] -> [Int] -> [Int]
bf seen forest | null forest = []
| otherwise = forest ++
bf (forest ++ seen) (concat (map goDeeper forest))
where goDeeper v = if elem v seen then [] else (g ! v)
graph = buildG bounds edges
where bounds = (1,7)
edges = [ (1,2), (1,5)
, (2,3), (2,4)
, (5,6), (5,7)
, (3,1) ]

View File

@ -0,0 +1,5 @@
default:
cabal install --bindir=./
clean:
rm -rf dist Setup.hs Code0605 *~

View File

@ -0,0 +1,25 @@
-- Initial Code0506.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: Code0606
version: 0.1.0.0
synopsis: Visualizing a graph.
-- description:
homepage: haskelldata.com
license: GPL-2
license-file: ../LICENSE
author: Nishant Shukla
maintainer: nick722@gmail.com
-- copyright:
category: Graphics
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable Code0606
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.6 && <4.7, graphviz >=2999.17 && <2999.18
-- hs-source-dirs:
default-language: Haskell2010

20
Ch06/Code06_viz/Main.hs Normal file
View File

@ -0,0 +1,20 @@
import Data.GraphViz
-- Visualizing a graphical network
main = addExtension (runGraphviz graph) Png "graph"
graph :: DotGraph Int
graph = graphElemsToDot graphParams nodes edges
graphParams :: GraphvizParams Int String Bool () String
graphParams = defaultParams
nodes :: [(Int, String)]
nodes = map (\x -> (x, "")) [1..4]
edges :: [(Int, Int, Bool)]
edges = [ (1, 3, True)
, (1, 4, True)
, (2, 3, True)
, (2, 4, True)
, (3, 4, True)]

5
Ch06/Code06_viz/Makefile Normal file
View File

@ -0,0 +1,5 @@
default:
cabal install --bindir=./
clean:
rm -rf dist Setup.hs Code0606 *.png *~

View File

@ -0,0 +1,25 @@
-- Initial Code0507.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: Code0607
version: 0.1.0.0
synopsis: Constructing a DAWG.
-- description:
homepage: haskelldata.com
license: GPL-2
license-file: ../LICENSE
author: Nishant Shukla
maintainer: nick722@gmail.com
-- copyright:
category: Data
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable Code0607
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.6 && <4.7, dawg >=0.11 && <0.12, HTTP >=4000.2 && <4000.3, deepseq >=1.3 && <1.4
-- hs-source-dirs:
default-language: Haskell2010

33
Ch06/Code07_dawg/Main.hs Normal file
View File

@ -0,0 +1,33 @@
import qualified Data.DAWG.Static as D
import Network.HTTP (simpleHTTP, getRequest, getResponseBody)
import Data.Char (toLower, isAlphaNum, isSpace)
import Data.Maybe (isJust)
import System.CPUTime
import Data.List (isInfixOf)
import Control.DeepSeq (deepseq)
-- Querying strings using a directed acyclic word graph (DAWG)
main = do
let url = "http://norvig.com/big.txt"
body <- simpleHTTP (getRequest url) >>= getResponseBody
let corp = corpus body
print $ isJust $ D.lookup "hello" corp
start <- getCPUTime
print $ isJust $ D.lookup "goodbye" corp
end <- getCPUTime
let diff = (fromIntegral (end - start)) / (10^12)
print diff
print $ "hello" `isInfixOf` body
start <- getCPUTime
print $ "goodbye" `isInfixOf` body
end <- getCPUTime
let diff = (fromIntegral (end - start)) / (10^12)
print diff
getWords :: String -> [String]
getWords str = words $ map toLower wordlike
where wordlike = filter (\x -> isAlphaNum x || isSpace x) str
corpus :: String -> D.DAWG Char () ()
corpus str = D.fromLang $ getWords str

View File

@ -0,0 +1,5 @@
default:
cabal install --bindir=./
clean:
rm -rf dist Setup.hs Code0607 *~

View File

@ -0,0 +1,25 @@
-- Initial Code0508.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: Code0608
version: 0.1.0.0
synopsis: Dealing with grid-like graphs.
-- description:
homepage: haskelldata.com
license: GPL-2
license-file: ../LICENSE
author: Nishant Shukla
maintainer: nick722@gmail.com
-- copyright:
category: Data
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable Code0608
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.6 && <4.7, grid >=7.6 && <7.7
-- hs-source-dirs:
default-language: Haskell2010

25
Ch06/Code08_grid/Main.hs Normal file
View File

@ -0,0 +1,25 @@
import Math.Geometry.Grid (indices, neighbours)
import Math.Geometry.Grid.Hexagonal (hexHexGrid)
import Math.Geometry.Grid.Square (rectSquareGrid)
import Math.Geometry.GridMap ((!))
import Math.Geometry.GridMap.Lazy (lazyGridMap)
-- Using various grids
main = do
let putStrLn' str = putStrLn ('\n':str)
putStrLn' "Indices of hex grid:"
print $ indices hex
putStrLn' "Neighbors around (1,1) of hex grid:"
print $ neighbours hex (1,1)
putStrLn' "Indices of rect grid:"
print $ indices rect
putStrLn' "Neighbors around (1,1) of rect grid:"
print $ neighbours rect (1,1)
putStrLn' "value of hex at index (1,1)"
print $ hexM ! (1,1)
hex = hexHexGrid 4
rect = rectSquareGrid 3 5
hexM = lazyGridMap hex [1..]

View File

@ -0,0 +1,5 @@
default:
cabal install --bindir=./
clean:
rm -rf dist Setup.hs Code0608 *~

View File

@ -0,0 +1,25 @@
-- Initial Code0509.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: Code0609
version: 0.1.0.0
synopsis: Finding the maximial clique.
-- description:
homepage: haskelldata.com
license: GPL-2
license-file: ../LICENSE
author: Nishant Shukla
maintainer: nick722@gmail.com
-- copyright:
category: Data
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable Code0609
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.6 && <4.7, maximal-cliques >=0.1 && <0.2
-- hs-source-dirs:
default-language: Haskell2010

15
Ch06/Code09_cliq/Main.hs Normal file
View File

@ -0,0 +1,15 @@
import Data.Algorithm.MaximalCliques
main = do
print $ getMaximalCliques edges nodes
edges 1 5 = True
edges 1 2 = True
edges 2 3 = True
edges 2 5 = True
edges 4 5 = True
edges 3 4 = True
edges 4 6 = True
edges _ _ = False
nodes = [1..6]

View File

@ -0,0 +1,5 @@
default:
cabal install --bindir=./
clean:
rm -rf dist Setup.hs Code0609 *~

View File

@ -0,0 +1,25 @@
-- Initial Code0510.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: Code0610
version: 0.1.0.0
synopsis: Determining isomorphic graphs.
-- description:
homepage: haskelldata.com
license: GPL-2
license-file: ../LICENSE
author: Nishant Shukla
maintainer: nick722@gmail.com
-- copyright:
category: Data
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
executable Code0610
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=4.6 && <4.7, containers >=0.5 && <0.6, hgal >=2.0 && <2.1
-- hs-source-dirs:
default-language: Haskell2010

14
Ch06/Code10_auto/Main.hs Normal file
View File

@ -0,0 +1,14 @@
import Data.Graph
import Data.Graph.Automorphism
main = print $ isIsomorphic graph graph'
graph = buildG (0,4) [ (1, 3), (1, 4)
, (1, 2), (2, 3)
, (2, 4), (3, 4) ]
graph' = buildG (0,4) [ (3, 1), (3, 2)
, (3, 4), (4, 1)
, (4, 2), (1, 2) ]

View File

@ -0,0 +1,5 @@
default:
cabal install --bindir=./
clean:
rm -rf dist Setup.hs Code0610 *~

View File

@ -6,8 +6,19 @@ Refer to the book for step-by-step explanations.
<p align="center"><a href="http://haskelldata.com" target="_blank"><img src="http://haskelldata.com/images/ch06.png"/></a></p>
# How to use
# Recipes:
* **Code01**: Representing a graph from a list of edges
* **Code02**: Representing a graph from an adjacency list
* **Code03**: Conducting a topological sort on a graph
* **Code04**: Traversing a graph depth-first
* **Code05**: Traversing a graph breadth-first
* **Code06**: Visualizing a graph using Graphviz
* **Code07**: Using Directed Acyclic Word Graphs
* **Code08**: Working with hexagonal and square grid networks
* **Code09**: Finding maximal cliques in a graph
* **Code10**: Determining whether any two graphs are isomorphic
# How to use
### Setting up the environment
Install the [Haskell Platform](http://www.haskell.org/platform/).