topological sort using c++ as programming language to search through a tree structureknljhcffxgchjkjhlkjkfhdffxgchvjbknlkjhgchvjbkjxfghjhiyuighjbyfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffvgvvvvvvvvnbbbbbbbbbfuyfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
graphin-c1.png
graphin-c1.txt
1: 2
2: 3 8
3: 4
4: 5
5: 3
6: 7
7: 3 6 8
8: 1 9
9: 1
graphin-c2.jpg
graphin-c2.txt
1: 2 9
2: 3 8
3: 4
4: 5 9
5: 3
6: 7
7: 3 6 8
8: 1
9:
graphin-DAG.png
graphin-DAG.txt
1: 2
2: 3 8
3: 4
4: 5
5: 9
6: 4 7
7: 3 8
8: 9
9:
CS 340 Programming Assignment III:
Topological Sort
Description: You are to implement the Depth-First Search (DFS) based algorithm for (i)
testing whether or not the input directed graph G is acyclic (a DAG), and (ii) if G is a DAG,
topologically sorting the vertices of G and outputting the topologically sorted order.
I/O Specifications: You will prompt the user from the console to select an input graph
filename, including the sample file graphin.txt as an option. The graph input files must be of
the following adjacency list representation where each xij is the j'th neighbor of vertex i (vertex
labels are 1 through n):
1: x11 x12 x13 ...
2: x21 x22 x23 ...
.
.
n: xn1 xn2 xn3 ...
Your output will be to the console. You will first output whether or not the graph is acyclic. If
the graph is NOT acyclic, then you will output the set of back edges you have detected during
DFS. Otherwise, if the graph is acyclic, then you will output the vertices in topologically
sorted order.
Algorithmic specifications:
Your algorithm must use DFS appropriately and run in O(E + V) time on any input graph. You will
need to keep track of edge types and finish times so that you can use DFS for detecting
cyclicity/acyclicity and topologically sorting if the graph is a DAG. You may implement your graph
class as you wish so long as your overall algorithm runs correctly and efficiently.
What to Turn in: You must turn in a single zipped file containing your source code, a Makefile
if your language must be compiled, appropriate input and output files, and a README file
indicating how to execute your program (especially if not written in C++ or Java). Refer to
proglag.pdf for further specifications.
This assignment is due by MIDNIGHT of Monday, February 19. Late submissions
carry a minus 40% per-day late penalty.
Sheet1Name:Possible:Score:Comments:10Graph structure with adjacency list representationDFS16Correct and O(V+E) time10Detecting cycles, is graph DAG?Topological Sort16Correctness of Topo-Sort algorithm and output18No problems in compilation and execution? Non-compiling projects receive max total 10 points, and code that compiles but crashes during execution receives max total 18 points.700Total
&"Helvetica,Regular"&12&K000000&P
Sheet2
&"Helvetica,Regular"&12&K000000&P
Sheet3
&"Helvetica,Regular"&12&K000000&P
DFS and topological sort
CS340
Depth first search
breadth
depth
Search "deeper" whenever possible
*example shows discovery times
Depth first search
Input: G = (V,E), directed or undirected.
No source vertex is given!
Output: 2 timestamps on each vertex:
v.d discovery time
v.f finishing time
These will be useful ...
graphin-c1.png
graphin-c1.txt
1: 2
2: 3 8
3: 4
4: 5
5: 3
6: 7
7: 3 6 8
8: 1 9
9: 1
graphin-c2.jpg
graphin-c2.txt
1: 2 9
2: 3 8
3: 4
4: 5 9
5: 3
6: 7
7: 3 6 8
8: 1
9:
graphin-DAG.png
graphin-DAG.txt
1: 2
2: 3 8
3: 4
4: 5
5: 9
6: 4 7
7: 3 8
8: 9
9:
CS 340 Programming Assignment III:
Topological Sort
Description: You are to implement the Depth-First Search (DFS) based algorithm for (i)
testing whether or not the input directed graph G is acyclic (a DAG), and (ii) if G is a DAG,
topologically sorting the vertices of G and outputting the topologically sorted order.
I/O Specifications: You will prompt the user from the console to select an input graph
filename, including the sample file graphin.txt as an option. The graph input files must be of
the following adjacency list representation where each xij is the j'th neighbor of vertex i (vertex
labels are 1 through n):
1: x11 x12 x13 ...
2: x21 x22 x23 ...
.
.
n: xn1 xn2 xn3 ...
Your output will be to the console. You will first output whether or not the graph is acyclic. If
the graph is NOT acyclic, then you will output the set of back edges you have detected during
DFS. Otherwise, if the graph is acyclic, then you will output the vertices in topologically
sorted order.
Algorithmic specifications:
Your algorithm must use DFS appropriately and run in O(E + V) time on any input graph. You will
need to keep track of edge types and finish times so that you can use DFS for detecting
cyclicity/acyclicity and topologically sorting if the graph is a DAG. You may implement your graph
class as you wish so long as your overall algorithm runs correctly and efficiently.
What to Turn in: You must turn in a single zipped file containing your source code, a Makefile
if your language must be compiled, appropriate input and output files, and a README file
indicating how to execute your program (especially if not written in C++ or Java). Refer to
proglag.pdf for further specifications.
This assignment is due by MIDNIGHT of Monday, February 19. Late submissions
carry a minus 40% per-day late penalty.
Sheet1Name:Possible:Score:Comments:10Graph structure with adjacency list representationDFS16Correct and O(V+E) time10Detecting cycles, is graph DAG?Topological Sort16Correctness of Topo-Sort algorithm and output18No problems in compilation and execution? Non-compiling projects receive max total 10 points, and code that compiles but crashes during execution receives max total 18 points.700Total
&"Helvetica,Regular"&12&K000000&P
Sheet2
&"Helvetica,Regular"&12&K000000&P
Sheet3
&"Helvetica,Regular"&12&K000000&P
DFS and topological sort
CS340
Depth first search
breadth
depth
Search "deeper" whenever possible
*example shows discovery times
Depth first search
Input: G = (V,E), directed or undirected.
No source vertex is given!
Output: 2 timestamps on each vertex:
v.d discovery time
v.f finishing time
These will be useful ...
The Impact of Artificial Intelligence on Modern Society.pdfssuser3e63fc
Just a game Assignment 3
1. What has made Louis Vuitton's business model successful in the Japanese luxury market?
2. What are the opportunities and challenges for Louis Vuitton in Japan?
3. What are the specifics of the Japanese fashion luxury market?
4. How did Louis Vuitton enter into the Japanese market originally? What were the other entry strategies it adopted later to strengthen its presence?
5. Will Louis Vuitton have any new challenges arise due to the global financial crisis? How does it overcome the new challenges?Assignment 3
1. What has made Louis Vuitton's business model successful in the Japanese luxury market?
2. What are the opportunities and challenges for Louis Vuitton in Japan?
3. What are the specifics of the Japanese fashion luxury market?
4. How did Louis Vuitton enter into the Japanese market originally? What were the other entry strategies it adopted later to strengthen its presence?
5. Will Louis Vuitton have any new challenges arise due to the global financial crisis? How does it overcome the new challenges?Assignment 3
1. What has made Louis Vuitton's business model successful in the Japanese luxury market?
2. What are the opportunities and challenges for Louis Vuitton in Japan?
3. What are the specifics of the Japanese fashion luxury market?
4. How did Louis Vuitton enter into the Japanese market originally? What were the other entry strategies it adopted later to strengthen its presence?
5. Will Louis Vuitton have any new challenges arise due to the global financial crisis? How does it overcome the new challenges?
This comprehensive program covers essential aspects of performance marketing, growth strategies, and tactics, such as search engine optimization (SEO), pay-per-click (PPC) advertising, content marketing, social media marketing, and more
2. For a directed acyclic graph G=(V,E)
A topological sort is an ordering of all of G’svertices
v1, v2, v3,….,vn such that …
Vertex u comes before vertex v if edge (u,v) belongs to G
So,formally for every egde (vi,vk) in E ,i<k
WHAT IS TOPOLOGICAL SORT ?
3. TOPOLOGICAL SORT
• Definition:
A topological sort or topological ordering of a directed ayclic
graph is a linear ordering of its vertices such that for every
directed edge (u,v) from vertex u to v, u comes before v in the
ordering.
4. 0 1
5
2
4
3
• There can be more then one
topological sorting for a graph.
• A topological sorting of the given
graph can be
• “5 4 2 3 1 0”
• “4 5 2 3 1 0”
• “5 2 3 4 1 0”
• “5 2 3 4 0 1”
• The first vertex in topological
sorting is always a vertex with
in-degree as “0”
• ( a vertex with no incoming edges).
5. Topological sorting Application
• The vertices of the graph may represent tasks to be performed, and the
edges may represent constraints that one task must be performed before
another;
• In this application, a topological ordering is just a valid sequence for the
tasks.
• A topological ordering is possible if and only if the graph has no
directed cycles, that is, if it is a directed acyclic graph (DAG).
8. Algorithms
Kahn's Algorithm
One of these algorithms, first described by Kahn (1962) works by choosing vertices
in the same order as the eventual topological sort.
L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edge
while S is non-empty do
remove a node n from S
add n to tail of L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least one cycle)
else
return L (a topologically sorted order)
If the graph is a DAG (Directed Acyclic Graph) a solution will be contained in the list L
9. Example
1 2
4
3 5
6 7
Identify nodes having in degree ‘0’
Select a node and delete it with its
edges then add node to output
Select Node : 1
Output :
1
10. Contd…
2
4
3 5
6 7
Identify nodes having in degree ‘0’
Select a node and delete it with its
edges then add node to output
Select Node : 2
Output :
1 2
11. Contd…
4
3 5
6 7
Identify nodes having in degree ‘0’
Select a node and delete it with its
edges then add node to output
Select Node : 5
Output :
1 2 5
12. Contd…
4
3
6 7
Identify nodes having in degree ‘0’
Select a node and delete it with its
edges then add node to output
Select Node : 4
Output :
1 2 5 4
13. Contd…
3
6 7
Identify nodes having in degree ‘0’
Select a node and delete it with its
edges then add node to output
Select Node : 3
Output :
1 2 5 4 3
14. Contd…
6 7
Identify nodes having in degree ‘0’
Select a node and delete it with its
edges then add node to output
Select Node : 6, 7
Output :
1 2 5 4 3 6 7
1 2
4
3 5
6 7
15. Topological sort using DFS
Topological_Sort(G)
1. Call DFS(G) to compute finishing time v.f for each vertex v.
2. As each vertex is finished, insert it into the front of a linked list.
3. Return the linked list of vertices.
Time complexity : 𝛳(V+E) as DFS takes 𝛳(V+E) time.
It takes O(1) time to insert each of the |V| vertices onto the front of the
linked list.
16. Example
e a b d f g c h i
a e
d
b
c g
h
i
f
11/16
12/15
6/7
1/8
2/5
3/4
17/18
13/14
9/10
DFS Traversal
3/4
2/5
6/7
1/8 9/10
13/14
12/15
11/16
17/18
17. Strongly Connected
Components
A strongly connected component of a directed graph
G=(V,E) is a maximal set of vertices such that
for every pair of vertices u and v in C , we have both
u~v and v~u
C V
18. STRONGLY-CONNECTED-COMPONENTS(G)
1. call DFS(G) to compute finishing times f[u] for each vertex u
2. compute GT
GT = (V, ET), where ET = {(u,v): (v,u)ϵ E}. That is, ET consists of
the edges of G with their directions reversed.
3. call DFS(GT), but in the main loop of DFS, consider the vertices
in order of decreasing f[u] (as computed in line 1)
4. output the vertices of each tree in the depth-first forest formed in
line 3 as a separate strongly connected component
19. (a) A directed graph G. Each shaded
region is a strongly connected
component of G. Each vertex is
labeled with its discovery and
finishing times in a depth-first search,
and tree edges are shaded.
(b) The graph GT, the transpose of G,
with the depth-first forest computed
in line 3 of STRONGLY-
CONNECTED-COMPONENTS
shown and tree edges shaded. Each
strongly connected component
corresponds to one depth-first tree.
Vertices b, c, g, and h, which are
heavily shaded, are the roots of the
depth-first trees produced by the
depth-first search of GT.
(c) The acyclic component graph GSCC
obtained by contracting all edges
within each strongly connected
component of G so that only a single
vertex remains in each component.
20. Strongly Connected Components
The transpose of a graph G, GT=(V,ET) is used for finding the strongly
connected components of a graph G=(V,E)
It is interesting to observe that G and GT have exactly the same strongly
connected components
21. • The complexity of algorithm is 𝛳(V+E).
Strongly Connected Components