0% found this document useful (0 votes)
11 views9 pages

Algorithm Homework 4 Solutions

This document contains solutions to homework problems about algorithms. It discusses depth-first search and finding negative length cycles. One problem asks to describe a process on a directed graph using vertex a as the source. Another asks when Dijkstra's algorithm will not work and gives the example where it fails to find the shortest path from A to C when a negative edge is present. The document also contains solutions running the DAG-SHORTEST-PATHS algorithm on a sample graph and an algorithm to determine if an undirected graph contains a cycle in O(V) time using depth-first search.

Uploaded by

woogie boogie
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Algorithm Homework 4 Solutions

This document contains solutions to homework problems about algorithms. It discusses depth-first search and finding negative length cycles. One problem asks to describe a process on a directed graph using vertex a as the source. Another asks when Dijkstra's algorithm will not work and gives the example where it fails to find the shortest path from A to C when a negative edge is present. The document also contains solutions running the DAG-SHORTEST-PATHS algorithm on a sample graph and an algorithm to determine if an undirected graph contains a cycle in O(V) time using depth-first search.

Uploaded by

woogie boogie
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Algorithm 2017 Spring

Homework 4 Solutions

指導教授 : 謝孫源 教授
助教 : 許景添 陳琮皓 林玉陞 何岱璇
1. Depth-First-Search algorithm
the timestamp (discovery time & finish time)
2.
After forming the augmented constraint graph and
seeking the shortest path from node 0 to all other
nodes, using an algorithm with negative length
cycle detection, one finds there is a negative length
cycle (2, 3, 5, 4, 2) with length 1 − 7 + 10 − 6 = −2.
Thus the system is infeasible.
3a. (10pts) Describe such a process clearly on the
following di-graph with vertex a as the source.
3b. (10pts) Under what condition Dijkstra’s algorithm
will not work? Given an example to explain your
answer.
在有negative edge 時Dijkstra’s algorithm 可能失效
EX:

用Dijkstra’s algorithm時得到A到C的最短路徑為(A → C)
但實際答案為(A → B → C)
[Link] DAG-SHORTEST-PATHS step by step on the
directed graph of the figure, using vertex s as the
source. (10%)
4. Run DAG-SHORTEST-PATHS step by step on the
directed graph of the figure, using vertex s as the
source. (10%)
5. Give an algorithm that determines whether or not a given
undirected graph G = (V ,E) contains a cycle. Your algorithm
should run in O(V) time, independent of |E|.
◦ An undirected graph is acyclic (i.e., a forest) if and only if a DFS yields no
back edges.
◦ If there is a back edge, there is a cycle.
◦ If there is no back edge, then by Theorem 22.10, there are only tree edges.
◦ Hence, the graph is acyclic.
◦ Thus, we can run DFS: if we find a back edge, there is a cycle.
◦ Time: O(V).(We can simply DFS. If find a back edge, there is a cycle. The
complexity is O(V) instead of O(E + V). Since if there is a back edge, it must
be found before seeing |V | distinct edges. This is because in a acyclic
(undirected ) forest, |E| ≤ |V | - 1, If it has back edge, |E| ≤ |V |)
5. Give an algorithm that determines whether or not a given
undirected graph G = (V ,E) contains a cycle. Your algorithm
should run in O(V) time, independent of |E|.

Common questions

Powered by AI

Topological sorting is notably relevant in the context of DAGs as it provides an order in which vertices, and consequently the tasks or events they represent, can be linearly arranged respecting dependency constraints. This acyclic arrangement allows for structured processing where each task can be completed with all its prerequisites, enabling algorithms like DAG-SHORTEST-PATHS to efficiently calculate shortest paths. The absence of cycles ensures that each step in processing a vertex is definitive and non-reversible, thereby simplifying the path optimization problem .

Discovery and finish times in DFS are crucial for tracking the progression of a traversal. Each vertex is assigned a discovery time when it is first encountered and a finish time when the traversal of its adjacency list is complete. These timestamps are useful for identifying back edges, classifying edges, and detecting cycles. Additionally, they can be used to strongly connect components and for topological sorting of directed acyclic graphs (DAGs).

In an undirected graph, if DFS yields no back edges, it indicates the absence of any cycles. This is because a back edge is an indicator that connects a vertex to one of its ancestors, forming a loop or cycle. Without any back edges, there is no connection that leads back to a previous vertex, and hence the graph can only consist of tree edges which do not form cycles. By verifying the absence of back edges during DFS, it is concluded that the graph is acyclic or a forest .

The complexity of a graph cycle detection algorithm can be made independent of the edge count when using Depth-First-Search (DFS) in an undirected graph. This is because the detection relies on the discovery of back edges, which suffice in identifying cycles and only require traversal up to |V| - 1 edges in a forest (a cycle-free graph). Hence, by focusing on vertices and the immediate connections made during DFS, the complexity remains O(V), not requiring engagement with all potential edges .

A negative length cycle in an augmented constraint graph implies an infeasible system because it indicates that there is no bounded solution to the problem. In such graphs, specific paths between nodes represent possible solutions, and a negative cycle suggests that repeatedly traversing this cycle can reduce the path length indefinitely, implying that constraints can be cumulatively violated in a manner that makes the system unsolvable or unbounded. For example, the cycle (2, 3, 5, 4, 2) with a length of -2 in the given graph example indicates such infeasibility .

Dijkstra's algorithm can become ineffective in the presence of negative weight edges because it assumes that once a node's shortest path is found, it does not need to be updated. If negative edges are present, this assumption fails, leading to incorrect shortest path calculations. An example of this is when computing the shortest path from A to C. If the shortest path initially determined by Dijkstra's algorithm is (A → C) but a negative edge exists between other nodes, the actual shortest path could be (A → B → C), which Dijkstra's algorithm would fail to identify due to its greedy assumption .

In a directed acyclic graph (DAG), shortest-path algorithms can efficiently compute paths from a source because such graphs have a topological ordering. Algorithms like DAG-SHORTEST-PATHS exploit this ordering to systematically process each vertex in sequence, ensuring that all necessary predecessors of a vertex are optimized before the vertex itself. This results in a time complexity that is linear with respect to the number of vertices and edges, making it particularly efficient for DAGs compared to general graphs where cycles could complicate computations .

A back edge during DFS traversal indicates a direct connection from a vertex to one of its ancestors in the DFS tree, thereby creating a loop or cycle. Since the graph traversal keeps track of ancestor vertices, encountering a back edge confirms that the initially traversed path can be cyclic when this backward link is considered. Hence, the presence of even a single back edge is enough to affirm that a cycle exists in the graph .

The primary algorithmic approach to determine if an undirected graph contains a cycle is the Depth-First-Search (DFS) method. This approach is preferred because it traverses the graph in a time-efficient manner and can immediately indicate the presence of cycles through back edges without processing all edges. The efficiency stems from identifying cycles based on direct ancestor connections within the traversal tree, keeping complexity to O(V) rather than depending heavily on edge count .

The DFS algorithm can be used to detect cycles in an undirected graph by identifying the presence of back edges, which point from a vertex to one of its ancestors in the DFS tree. If a DFS traversal yields any back edges, it indicates the presence of a cycle. The computational complexity of this cycle detection algorithm is O(V), where V is the number of vertices, because in an undirected graph, the DFS will check each vertex once and will identify back edges if they exist .

You might also like