Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement those two algorithms in the following code: import sys import random import collections This code is Part 1:Vertex Cover Approximation def main():

image text in transcribed

Implement those two algorithms in the following code:

import sys import random import collections

""" This code is Part 1:Vertex Cover Approximation """

def main(): filename = sys.argv[1] algo = sys.argv[2]

G = load_graph(filename)

cover = [] if algo == "greedy-vertex": cover = greedy_vertex(G) elif algo == "greedy-edge": cover = greedy_edge(G) else: print "Incorrect arguments" exit()

output_cover(cover)

def output_cover(cover): with open("approxOutput.txt", "w") as fout: for v in cover: fout.write(str(v)+" ")

def greedy_edge(adj_list): """ Implement your code for greedy edge here. It should return a list of vertices that covers every edge. """

def greedy_vertex(adj_list): """ Implement your code for greedy vertex here. It should return a list of vertices that covers every edge. """

def load_graph(filename): adj_list = collections.defaultdict(set) with open(filename, "r") as fin: for line in fin: u, v = [int(v) for v in line.strip().split()] adj_list[u].add(v) adj_list[v].add(u) return adj_list

if __name__ == "__main__": main()

Algorithm 2- Approximation Greedy Vertex INPUT: A graph G-(V,E) OUTPUT: A vertex cover in G that is at most twice the size of a minimum VC 1. 2. While El > 0 1. Select the highest degree vertex v in G 3. Remove v and all adjacent edges from G 3. Return C INPUT: A graph G-(V,E) OUTPUT: A vertex cover in G that is at most twice the size of a minimum VC 2. While IEI>0 1. Select an edge (u,v) in E Remove any edges incident to u or v in E 3. 3. Return C Algorithm 2- Approximation Greedy Vertex INPUT: A graph G-(V,E) OUTPUT: A vertex cover in G that is at most twice the size of a minimum VC 1. 2. While El > 0 1. Select the highest degree vertex v in G 3. Remove v and all adjacent edges from G 3. Return C INPUT: A graph G-(V,E) OUTPUT: A vertex cover in G that is at most twice the size of a minimum VC 2. While IEI>0 1. Select an edge (u,v) in E Remove any edges incident to u or v in E 3. 3. Return C

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_2

Step: 3

blur-text-image_3

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

More Books

Students also viewed these Databases questions

Question

Decide on best content

Answered: 1 week ago

Question

b. Will new members be welcomed?

Answered: 1 week ago