Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. # Programmed by Robert Brodin. January 2017. Depth-First Search Algorithms Final Project. I might have gone overboard with the comments, *cough*
  2. def dfs(graph, source):
  3.  
  4. """
  5. Defines a function named DFS with arguments graph, a dictionary (contains keys E and V), and source, the starting point of the graph. The function will use the Depth-First Search Algorithm to find the time it takes to discover the vertice (discoveryTime), and the amount of time it takes to turn to the color black (finishTime). The function will return a dictionary, vertexInfo.
  6. """
  7.  
  8. def adjacentVertex(edge, unwantedVertex):
  9.  
  10. """
  11. Defines a function named adjacentVertex with arguments edge, a list, and the unwantedVertex, an integer. The function finds the other vertex in a list when given a vertex. The function returns an integer.
  12. """
  13.  
  14. for vertice in edge:
  15. if vertice != unwantedVertex:
  16. return vertice
  17.  
  18. def backTrack(originalSource, currentSource):
  19.  
  20. """
  21. Defines a function named backTrack with arguments originalSource, the source specified at the start of the main function, DFS, and currentSource, the vertex that the finishTime is currently being calculated for. The function will find the finishTime, or the time it takes for the vertex to become black. The function does not return any values, it just edits the main dictionary, vertexInfo. Also, if you noticed, recursion is used. :)
  22. """
  23.  
  24. # Checks if the source equals the currentSource. Is used to check if when backtracking, the currentSource is finally the point you started from originally.
  25.  
  26. if originalSource != currentSource:
  27.  
  28. # Updates dictionary vertexInfo with finishTime. finishTime is calculated by looking at the first index of the list descendants, and at that descendants finishTime. The currentSource's finishTime is set to it's descendants finishTime plus one. Side note, in our case, we are not adding weight to certain vertices, so the descendants will always have the same finishTime in the case of this problem. Meaning that we can use index 0 of the descendants list to get the finish time.
  29.  
  30. vertexInfo[str(currentSource)]['finishTime'] = vertexInfo[str(vertexInfo[str(currentSource)]['descendants'][0])]['finishTime'] + 1
  31.  
  32. # Color is set to black, as there are no other undiscovered vertices coming from this vertex.
  33.  
  34. vertexInfo[str(currentSource)]['color'] = 'black'
  35.  
  36. # Recursion is used to keep backtracking from ancestor to ancestor. It will eventually stop once the backtracking reaches the source.
  37. backTrack(originalSource, vertexInfo[str(currentSource)]['ancestor'])
  38.  
  39. # vertexInfo is used to store information about each vertex. The information contained is the color, finishTime, ancestor, descendants, and discoveryTime.
  40. vertexInfo = {}
  41.  
  42. # List vertexQueue is used to store the vertices connected to the source, and later will be used to store vertices recently discovered.
  43. vertexQueue = []
  44.  
  45. # List discoveredVertices will contain vertices that have been discovered. It will be used to check if vertices have been discovered.
  46. discoveredVertices = [source]
  47.  
  48. # Much like discoveredVertices, list discoveredEdges will contain edges that have been discovered. It will be used to check if edges have been discovered, this list is essential in the backtracking process.
  49. discoveredEdges = []
  50.  
  51. # Loops through each vertex in the graph at key 'V'. Sets up each vertices dictionary. Can be used to tell if one vertex is seperate from the main vertices.
  52. for vertex in graph['V']:
  53. vertexInfo[str(vertex)] = {'color': 'white', 'discoveryTime': 0, 'finishTime': 0, 'ancestor': '', 'descendants': []}
  54.  
  55. # Loops through each edge in the graph. This loop is used to find each vertice connected to the source. If the source is in the edge, then data is then updated. The sources discoveryTime is set to zero, as it will always be zero, and the discovered vertex is then appended to the list of descendants for the source key.
  56. for edge in graph["E"]:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement