Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.1 : graph =[('a','b')] should produce: ("a","b")
ex.2: graph =[('a','b'),('b','c')] produce: ("","")
ex.3: graph =[('a','b'),('b','c'),('c','a')] produce: ("","")
ex.4: graph =[('a','b'),('b','c'),('c','d'),('d','a')] produce: ("ac","bd")
ex.5: graph =[('c','b'),('a','b'),('a','d'),('d','c')] produce: ("ac","bd")
ex.6: graph =[('1','2'),('2','3'),('3','4'),('4','1')] produce: ("13","24")
ex.7: graph =[('a','f'),('a','h'),
('b','g'),('b','j'),
('c','f'),('c','i'),('c','j'),
('d','f'),('d','g'),
('e','h'),('e','j')] produce: ("abcde", "fghij")
ex.8: graph =[('a','f'),('a','h'),
('b','g'),('b','j'),
('c','f'),('c','i'),('c','j'),
('d','f'),('d','g'),
('e','h'),('e','j'),('a','e')] produce: ("","")
It can not use monads or data structures, it can use functions. It must use this function: bipartite :: [(Char, Char)]->([Char],[Char]). Here is what I currently have:
module Bipartite (bipartite) where
import Data.List
bipartite :: [(Char, Char)]->([Char],[Char])
--^Partitions graph g into two partitions if g is bipartite.
bipartite g =("","")
-- makes all tuples into list
allFirst :: [(Char, Char)]->[Char]
allFirst g =[fst x | x <- g]
allSecond :: [(Char, Char)]->[Char]
allSecond g =[snd x | x <- g]
tup :: [(Char, Char)]->[Char]
tup g =(allFirst g)++(allSecond g)
-- first =[fst x | x <- g]
-- second =[snd x | x <- g]
-- tup = first ++ second
partitionList =[]
removeDups :: [Char]->[Char]
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, Char)]->[Char]
neighbors v []=[]
neighbors v (g:gs)--v 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
-- partition[v]= opposite(partition[current])
assignPartition :: Char ->[Char]->[(Char,[Char])]->[(Char,[Char])]
assignPartition v p partitionList = partitionList ++[(v, p)]
-- line 10
addVtoList :: [(Char,[Char])]-> Char -> Char ->[Char]->[(Char,[Char])]
addVtoList partitionList v current s =(assignPartition v (opposite (depthFirst current partitionList)) partitionList)
-- line 11. function to update the stack
updateStack :: [Char]-> Char ->[(Char,[Char])]->([Char],[(Char,[Char])])
updateStack s v partitionList =((s ++[v]), partitionList)
-- lines 8-11
-- add neighbors to current partition
--assignAllNeighbors []=[]
-- Updated assignAllNeighbors to return [(Char,[Char])] instead of ([Char],[(Char,[Char])])
assignAllNeighbors :: Char -> Char ->[(Char,[Char])]->[Char]->[(Char,[Char])]
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 ->[Char]->[(Char,[Char])]->[Char]->[(Char,[Char])]
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 ->[(Char,[Char])]->[Char]
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 :: [Char]->[Char]
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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions

Question

=+working on a micro-multinational?

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago