Question: why doesnt my Haskell Bipartite Graph Partitioning Depth First Search Algorithm work? here are some examples it should produce given graph sets: ex . 1
why doesnt my Haskell Bipartite Graph Partitioning Depth First Search Algorithm work?
here are some examples it should produce given graph sets:
ex : graph ab should produce: ab
ex: graph abbc produce:
ex: graph abbcca produce:
ex: graph abbccdda produce: acbd
ex: graph cbabaddc produce: acbd
ex: graph produce:
ex: graph afah
bgbj
cfcicj
dfdg
ehej produce: abcde "fghij"
ex: graph afah
bgbj
cfcicj
dfdg
ehejae produce:
It can not use monads or data structures, it can use functions. It must use this function: bipartite :: Char CharCharChar Here is what I currently have:
module Bipartite bipartite where
import Data.List
bipartite :: Char CharCharChar
Partitions graph g into two partitions if g is bipartite.
bipartite g
makes all tuples into list
allFirst :: Char CharChar
allFirst g fst x x g
allSecond :: Char CharChar
allSecond g snd x x g
tup :: Char CharChar
tup g allFirst gallSecond g
first fst x x g
second snd x x g
tup first second
partitionList
removeDups :: CharChar
removeDups
removeDups x:xs
elem x xs removeDups xs
otherwise x:removeDups xs
function for finding neighbors of current
g:gs is graph
neighbors :: Char Char CharChar
neighbors v
neighbors v g:gsv is vertex we are looking the neighbor for
if the vertex eqauls to the first element, then add second elem to list
if the vertex equals to second elemnt, then add first elem to list
v fst g snd g:neighbors v gs
v snd g fst g:neighbors v gs
otherwise, keep going since we dont have the vertex we are looking for
otherwise neighbors v gs
partitionv oppositepartitioncurrent
assignPartition :: Char CharCharCharCharChar
assignPartition v p partitionList partitionList v p
line
addVtoList :: CharChar Char Char CharCharChar
addVtoList partitionList v current s assignPartition v opposite depthFirst current partitionList partitionList
line function to update the stack
updateStack :: Char Char CharCharCharCharChar
updateStack s v partitionList s v partitionList
lines
add neighbors to current partition
assignAllNeighbors
Updated assignAllNeighbors to return CharChar instead of CharCharChar
assignAllNeighbors :: Char Char CharCharCharCharChar
assignAllNeighbors current a partitionList s
depthFirst a partitionList addVtoList partitionList a current s
otherwise partitionList Return partitionList if depthFirst not empty
Updated loopFunc to correctly pass the partitionList to assignAllNeighbors
loopFunc :: Char CharCharCharCharCharChar
loopFunc partitionList partitionList
loopFunc current x:xs partitionList s loopFunc current xs updatedPartitionList s
where
updatedPartitionList assignAllNeighbors current x partitionList s
search function that finds partition for a particular vertex
search tuple list for the partition we are looking for and return the partition it is in
otherwise return nil
depthFirst :: Char CharCharChar
depthFirst v
depthFirst v x:xs
v fst x snd x if v is found in the first vertex then return partition of the vertex
otherwise depthFirst v xs
opposite uses search function
opposite :: CharChar
opposite side
if the side is left, then return right
side "left" "right" returns opposite
if the side is right, then return left
side "right" "left"
please test with the examples provided to ensure accuracy for an upvote
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
