Download Elementary Graph Algorithms - Analysis of Algorithms - Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!
Elementary Graph Algorithms
Graph representation
Given graph G = ( V , E ). In pseudocode, represent vertex set by G.V and edge set by G.E. - G may be either directed or undirected. - Two common ways to represent graphs for algorithms: - 1. Adjacency lists. - 2. Adjacency matrix. - When expressing the running time of an algorithm, it’s often in terms of both | V | and | E |. In asymptotic notation - and only in asymptotic notation – we’ll often drop the cardinality. Example O ( V + E ).
Adjacency lists
Adjacency Matrix
Implementing graph attributes
No one best way to implement. Depends on the programming language, the algorithm, and how the rest of the program interacts with the graph.
If representing the graph with adjacency lists, can represent vertex attributes in additional arrays that parallel the Adj array, e.g., d [1.. | V |], so that if vertices adjacent to u are in Adj [ u ] , store u.d in array entry d [ u ].
But can represent attributes in other ways. Example: represent vertex attributes as instance variables within a subclass of a Vertex class.
Breadth-first search
Example
Depth-first search
Depth-first search
Example
Theorem (Parenthesis theorem)
Theorem (White-path theorem)
Topological sort
Example
Dag of dependencies for putting on goalie equipment: