The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 2 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
YOU ONLY HAVE TO PLACE THE CODE WHERE IT SAYS <your code> in useGraph.py document that is attached. Written is a guidance document and an expected output.
Programming Activity 8 - Guidance
=================================
Files in the same folder
------------------------
Depending on which implementation you use for part 1,
you may need to include other files in the same folder.
Imports
-------
Depending on which implementation you use for part 1,
you may need to include additional import statement(s).
Part 1
------
This week's example files include 2 different implementations of this function.
Choose one and copy that implementation here.
Part 2
------
The courses and their prerequisites are shown in a comment.
The courses are each named with a single capital letter.
Part 3
------
The courses and their prerequisites are shown in a comment.
Part 4
------
Use an appropriate graph iterator.
Part 5
------
Use an appropriate graph iterator.
Part 6
------
If you used the stack-based implementation of the topologicalSort() function,
it can produce a different, correct ordering for each run of your program.
The following are some of the possible correct orderings:
B A C E D F G H
A C E B D F G H
A B C E D F G H
A B C D F E G H
B A C D F E G H
++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++
THIS IS THE EXPECTED OUTPUT Graph: 8 Vertices: H A B C E D F G 12 Edges: A>C:0 A>D:0 B>D:0 B>F:0 C>D:0 C>E:0 C>H:0 E>G:0 D>F:0 F>G:0 F>H:0 G>H:0 Courses: H A B C E D F G Prerequisites: A>C:0 A>D:0 B>D:0 B>F:0 C>D:0 C>E:0 C>H:0 E>G:0 D>F:0 F>G:0 F>H:0 G>H:0 One possible order to take the courses: B A C E D F G H
"""
File: abstractcollection.py
Copyright 2015 by Ken Lambert
"""
class AbstractCollection(object):
   """An abstract collection implementation."""
   # Constructor
   def __init__(self, sourceCollection = None):
       """Sets the initial state of self, which includes the
       contents of sourceCollection, if it's present."""
       self._size = 0
       self._modCount = 0
       if sourceCollection:
           for item in sourceCollection:
               self.add(item)
   # Accessor methods
   def isEmpty(self):
       """Returns True if len(self) == 0, or False otherwise."""
       return len(self) == 0
  Â
   def __len__(self):
       """Returns the number of items in self."""
       return self._size
   def __str__(self):
       """Returns the string representation of self, using []
       as delimiters."""
       return "[" + ", ".join(map(str, self)) + "]"
   def __eq__(self, other):
       """Returns True if self equals other,
       or False otherwise.
       Compares pairs of items in the two sequences
       generated by the iterators on self and other."""
       if self is other: return True
       if type(self) != type(other) or \
          len(self) != len(other):
           return False
       otherIter = iter(other)
       for item in self:
           if item != next(otherIter):
               return False
       return True
   def __add__(self, other):
       """Returns a new collection containing the contents
       of self and other."""
       result = type(self)(self)
       for item in other:
           result.add(item)
       return result
   def count(self, item):
       """Returns the number of instance of item in self."""
       counter = 0
       for nextItem in self:
           if item == nextItem: counter += 1
       return counter
   # These methods track and update the modCount, which is used to
   # prevent mutations within the context of an iterator (for loop)
   def getModCount(self):
       """Returns the number of modifications to the collection."""
       return self._modCount
   def incModCount(self):
       """Increments the number of modifications to the collection."""
       self._modCount += 1
Â
# This program exercises graphs.
Â
# Replace any "<your code>" comments with your own code statement(s)
# to accomplish the specified task.
# Do not change any other code.
Â
# The following files must be in the same folder:
#Â abstractcollection.py
#Â graph.py
Â
from graph import LinkedDirectedGraph
Â
# Part 1:
# Complete the following function:
def topologicalSort(graph):Â
   # <your code>
Â
graph = LinkedDirectedGraph()
Â
# The graph represents the following course prerequisites:
# A requires nothing
# B requires nothing
# C requires A
# D requires A, B, and C
# E requires C
# F requires B and D
# G requires E and F
# H requires C, F, and G
Â
# Part 2:
# Add the vertices:
# <your code>
Â
# Part 3:
# Add the edges:
# <your code>
Â
print("Graph:")
print(graph)
print()
Â
print("Courses:")
# Part 4:
# Display each vertex on a separate line:
# <your code>
print()
Â
print("Prerequisites:")
# Part 5:
# Display each edge on a separate line:
# <your code>
print()
Â
print("One possible order to take the courses:")
# Part 6:
# Display the courses in prerequisite (topological) order:
# <your code>
print()
Â
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly