0% found this document useful (0 votes)
38 views4 pages

Created by Utkarsharora On 3/3/17. /: E V Al Marked Stack V Al Marked Stack Al

The document describes an algorithm for topological sorting of a directed graph. It defines a TopologicalSortAlgo class that takes a graph as input, represented by an adjacency list. It performs a depth-first search (DFS) on the graph starting from each vertex, and pushes each finished vertex to a stack. The final order of vertices in the stack is a topological sort of the graph if it is acyclic. It provides methods to add edges to the graph and run the DFS-based algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

Created by Utkarsharora On 3/3/17. /: E V Al Marked Stack V Al Marked Stack Al

The document describes an algorithm for topological sorting of a directed graph. It defines a TopologicalSortAlgo class that takes a graph as input, represented by an adjacency list. It performs a depth-first search (DFS) on the graph starting from each vertex, and pushes each finished vertex to a stack. The final order of vertices in the stack is a topological sort of the graph if it is acyclic. It provides methods to add edges to the graph and run the DFS-based algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

package DataStructure.

graphs;
import java.io.*;
import java.util.*;

/**
* Created by utkarsharora on 3/3/17.
*/
public class TopologicalSortAlgo {

private int E, V;
private ArrayList<ArrayList<Integer>> al;
private boolean marked[];
private Stack<Integer> stack;

TopologicalSortAlgo(int V){
this.V = V;
al = new ArrayList<>();
marked = new boolean[V];
stack = new Stack<>();
for(int i=0;i<V;++i){
al.add(new ArrayList<>());
}
}

public void dfs(int vertex){


if(!marked[vertex]){
marked[vertex] = true;
for(int v:al.get(vertex)){
dfs(v);
}
stack.push(vertex);
}
}

public void addEdge(int from, int to){


al.get(from).add(to);
}

public static void main(String[] args){


//7 vertices in example digraph
TopologicalSortAlgo ts = new TopologicalSortAlgo(7);
ts.addEdge(0, 2);
ts.addEdge(0, 5);
ts.addEdge(0, 1);

ts.addEdge(3, 2);
ts.addEdge(3, 5);
ts.addEdge(3, 6);

ts.addEdge(6, 4);
ts.addEdge(6, 0);

ts.addEdge(1, 4);

for(int i=0;i<ts.V;++i) ts.dfs(i);

System.out.println("Topological Order : ");


while(!ts.stack.isEmpty()){
System.out.print(ts.stack.pop()+" ");
}
}
}

package stackproblems;

import java.util.EmptyStackException;

/**
* Created by utkarsharora on 3/13/17.
*/
public class FindMiddleStack<T> {
private Node<T> top;
private Node<T> middle;
private int size = 0;

private static class Node<T> {


Node<T> left;
T item;
Node<T> right;

Node(Node<T> left, T item, Node<T> next) {


this.left = left;
this.item = item;
this.right = next;
}
}

public void push(T item) {


final Node<T> node = new Node<T>(top, item, null);
if (top == null) {
top = node;
middle = node;
} else {
top.right = node;
top = node;
}

size++;
if (size % 2 == 0) {
middle = middle.right;
}
}

public T pop() {
if (top == null) {
throw new EmptyStackException();
}
T item = top.item;
top = top.left;
// important, to not leak references.
if (top != null) {
top.right= null;
}

if (top == null) {
middle = null;
}

if (size % 2 == 0) {
middle = middle.left;
}

size--;
return item;
}

public T peek() {
if (top == null) {
throw new EmptyStackException();
}
return top.item;
}

public T getMiddle() {
if (top == null) {
throw new EmptyStackException();
}
return middle.item;
}

public void deleteMiddle() {


if (top == null) {
throw new EmptyStackException();
}

if (middle.left != null) {
middle.left.right = middle.right;
}
if (middle.right != null) {
middle.right.left = middle.left;
}

if (size % 2 == 0) {
// deleted middle.
middle = middle.left;
} else {
middle = middle.right;
}

if (middle == null) {
top = null;
}
size--;
}

public int size() {


return size;
}

public boolean isEmpty() {


return size == 0;
}
}

You might also like