Difference between revisions of "Algorithms"

From Allan Gitobu
Jump to navigation Jump to search
Line 2: Line 2:
 
import java.util.LinkedList;
 
import java.util.LinkedList;
  
import com.sun.corba.se.impl.orbutil.graph.Graph;
+
public class Chart {
import com.sun.corba.se.impl.orbutil.graph.Node;
+
    static class Employee {
 +
        int id;
 +
        String firstName;
 +
        String lastName;
 +
        String jobTitle;
 +
        boolean mapped;
  
public class Graph{
+
        Employee(int id, String fn, String ln, String jt) {
 +
            this.id = id;
 +
            this.jobTitle = jt;
 +
            this.firstName = fn;
 +
            this.lastName = ln;
 +
        }
  
    static class Node{
+
         void map() {
        int value;
+
             mapped = true;
        String name;
 
        boolean visted;
 
 
 
         Node(int v){
 
             this.value = v;
 
            this.name = name;
 
 
         }
 
         }
  
         void visit(){
+
         void unmap() {
             visited = true;
+
             mapped = false;
 
         }
 
         }
 
        void unvisit(){
 
            visited = false;
 
        }dddd
 
 
     }
 
     }
  
    private final boolean directed;
+
     private final HashMap<Employee, LinkedList<Employee>> assignment;
     private final HashMap<Node, LinkedList<Node>> adjacencyMap;
 
  
     public Graph(boolean directed){
+
     public Chart(boolean directed) {
         this.directed = directed;
+
         assignment = new HashMap<>();
        adjacencyMap = new HashMap<>();
 
 
     }
 
     }
  
     public addEdgeHelper(Node a, Node b){
+
     public void addEdgeHelper(Employee a, Employee b) {
         LinkedList<Node> temp = adjacencyMap.get(a);
+
         LinkedList<Employee> e = assignment.get(a);
         if ((!tmp != null)) {
+
         if (e != null) {
             temp.remove(b)
+
             assignment.remove(b);
         } else {
+
         } else
             temp = new LinkedList<>();
+
             e = new LinkedList<>();
            temp.add(b);
+
        e.add(b);
            addEdgeHelper(a, temp);
+
        assignment.put(a, e);
        }
 
 
     }
 
     }
  
     public void addEdge(Node source, Node destination){
+
     public void addEdge(Employee boss, Employee subordinate) {
         if ((!adjacencyMap.keySet().contains(source))) {
+
         if (!assignment.containsKey(boss))
             adjacencyMap.put(source, null);
+
             assignment.put(boss, null);
        }
+
         if (!assignment.containsKey(subordinate))
         if ((!adjacencyMap.keySet().contains(desitnation))) {
+
             assignment.put(subordinate, null);
             adjacencyMap.put(destination, null);
+
         addEdgeHelper(boss, subordinate);
         }
 
        if (!directed) {
 
            addEdgeHelper(destination, source);
 
        }
 
 
     }
 
     }
  
     public void resetVisits(){
+
     public void depthFirstSearch(Employee employee) {
         for ((Node f : adjacencyMap.keySet())) {
+
         employee.map();
             f.unvisit();
+
        System.out.println(employee.firstName + " " + employee.lastName + " is a " + employee.jobTitle);
 +
        LinkedList<Employee> neighbors = assignment.get(employee);
 +
        if (neighbors == null)
 +
            return;
 +
        for (Employee e : neighbors) {
 +
             if (!e.mapped)
 +
                depthFirstSearch(e);
 
         }
 
         }
 
     }
 
     }
/*visit the given node, get its neighbors and recursively visit them*/
 
    public void depthFirstSearch(Node a){
 
        a.visit();
 
        System.out.println(a.name + " ")
 
        LinkedList<Node> neighbors = adjacencyMap.get(a);
 
  
         if (neighbors == null) {
+
    public void breadthFirstSearch(Employee employee) {
 +
        /*Check if target item is null - if so quit*/
 +
         if (employee == null)
 
             return;
 
             return;
         }
+
         /*Declare a queue using LinkedList and add the target item*/
 +
        LinkedList<Employee> queue = new LinkedList<>();
 +
        queue.add(employee);
 +
        /*As long as the queue has data get the first in item .removeFirst*/
 +
        while (!queue.isEmpty()) {
 +
            Employee currentFirst = queue.removeFirst();
 +
            /*If visited move on*/
 +
            if (currentFirst.mapped) {
 +
                continue;
 +
            } else {
 +
                /*else visit it and print it*/
 +
                currentFirst.map();
  
        for (Node neighbor : neighbors) {
+
                System.out.println(currentFirst.id + ": " + currentFirst.firstName + " " + currentFirst.lastName + " - " + currentFirst.jobTitle);
             if ((!neighbor.visted)) {
+
            }
                depthFirstSearch(neighbor);
+
            /*Find neighbors of the current item*/
 +
            LinkedList<Employee> allNeighbors = assignment.get(currentFirst);
 +
            /*If there are none move on*/
 +
            if (allNeighbors == null)
 +
                continue;
 +
             /*else if there are some and not visited, add them to the queue and the process starts over*/
 +
            for (Employee e : allNeighbors) {
 +
                if (!e.mapped)
 +
                    queue.add(e);
 
             }
 
             }
 
         }
 
         }
 
     }
 
     }
/*create a queue and add the given node. While not empty declare a current node as  variable and HashMap.get given node. Visit it. While not empty get neigbhors and visit them*/
+
 
     public void breadthFirstSearch(Node b){
+
     public void resetMapping() {
         if ((b == null)) {
+
         for (Employee e : assignment.keySet()) {
             return;
+
             e.unmap();
 
         }
 
         }
         LinkedList<Node> queue = new LinkedList<>();
+
    }
         queue.add(b);
+
 
 +
    public static void main(String args[]) {
 +
         Employee a = new Employee(38, "Jimbo", "Jones", "President");
 +
         Employee b = new Employee(39, "Mike", "McCobb", "VP");
 +
        Employee c = new Employee(43, "Elda", "Two", "VP");
 +
        Employee d = new Employee(48, "Simon", "Sneeth", "VP");
 +
        Employee e = new Employee(56, "Tim", "Scott", "Manager");
 +
        Employee f = new Employee(58, "Bob", "Tilman", "Manager");
  
         while ((!queue.isEmpty())) {
+
         Employee g = new Employee(78, "Andrew", "Cole", "Analyst");
            Node current = queue.removeFirst();
+
        Employee h = new Employee(83, "Matt", "Lemon", "Analyst");
            if (current.visted) {
+
        Employee i = new Employee(88, "Chris", "Lock", "Analyst");
                continue;
+
        Employee j = new Employee(71, "Agnes", "White", "Accountant");
            }
+
 
            current.visit();
+
        Employee k = new Employee(61, "George", "Mills", "Manager");
            System.out.println(current.name + " ");
+
        Employee l = new Employee(59, "Thomas", "Wells", "Manager");
            LinkedList<Node> allNeigbhors = adjacencyMap.get(b);
+
        Employee m = new Employee(63, "Zip", "Two", "Manager");
 +
        Employee n = new Employee(73, "Rasta", "Him", "Technician");
  
            if ((allNeigbhors == null)) {
+
        Employee o = new Employee(89, "Claire", "Collins", "Supervisor");
                continue;
+
        Employee p = new Employee(95, "Jim", "Four", "Receptionist");
            }
+
        Employee q = new Employee(104, "Tom", "Flat", "Receptionist");
  
            for (Node neighbor : allNeighbors) {
+
        Employee r = new Employee(64, "Grace", "Marks", "Manager");
                if ((!neighbor.visited)) {
+
        Employee s = new Employee(68, "Dan", "Four", "Manager");
                    queue.add(neighbor);
 
                }
 
            }
 
        }
 
    }
 
  
    public static void main (String args[]){
+
        Employee t = new Employee(117, "Shad", "Fifty", "Security");
         Graph graph = new Graph(true);
+
        Employee u = new Employee(108, "Tall", "Nephew", "Legal");
 +
         Employee v = new Employee(109, "Short", "Nephew", "Legal");
  
        Node a = new Node(1, "A");
 
        Node b = new Node(2, "B");
 
        Node c = new Node(3, "C");
 
        Node d = new Node(4, "D");
 
  
         graph.addEdge(a, b);
+
         Chart chart = new Chart(true);
         graph.addEdge(a, c);
+
        chart.addEdge(a, b);
 +
         chart.addEdge(a, c);
 +
        chart.addEdge(a, d);
 +
        chart.addEdge(b, e);
 +
        chart.addEdge(b, f);
 +
        chart.addEdge(e, g);
 +
        chart.addEdge(e, h);
 +
        chart.addEdge(e, i);
 +
        chart.addEdge(f, j);
 +
        chart.addEdge(c, k);
 +
        chart.addEdge(c, l);
 +
        chart.addEdge(c, m);
 +
        chart.addEdge(l, n);
 +
        chart.addEdge(k, o);
 +
        chart.addEdge(o, p);
 +
        chart.addEdge(o, q);
 +
        chart.addEdge(d, r);
 +
        chart.addEdge(d, s);
 +
        chart.addEdge(s, t);
 +
        chart.addEdge(r, u);
 +
        chart.addEdge(r, v);
 +
        //System.out.println(a.firstName + " " + a.lastName + " " + a.jobTitle + " " + a.id);
 +
        chart.resetMapping();
 +
        //chart.depthFirstSearch(k);
  
         graph.depthFirstSearch(a);
+
         chart.resetMapping();
         graph.breadthFirstSearch(b);
+
         chart.breadthFirstSearch(a);
 
     }
 
     }
 
}
 
}

Revision as of 01:46, 20 October 2022

import java.util.HashMap; import java.util.LinkedList;

public class Chart {

   static class Employee {
       int id;
       String firstName;
       String lastName;
       String jobTitle;
       boolean mapped;
       Employee(int id, String fn, String ln, String jt) {
           this.id = id;
           this.jobTitle = jt;
           this.firstName = fn;
           this.lastName = ln;
       }
       void map() {
           mapped = true;
       }
       void unmap() {
           mapped = false;
       }
   }
   private final HashMap<Employee, LinkedList<Employee>> assignment;
   public Chart(boolean directed) {
       assignment = new HashMap<>();
   }
   public void addEdgeHelper(Employee a, Employee b) {
       LinkedList<Employee> e = assignment.get(a);
       if (e != null) {
           assignment.remove(b);
       } else
           e = new LinkedList<>();
       e.add(b);
       assignment.put(a, e);
   }
   public void addEdge(Employee boss, Employee subordinate) {
       if (!assignment.containsKey(boss))
           assignment.put(boss, null);
       if (!assignment.containsKey(subordinate))
           assignment.put(subordinate, null);
       addEdgeHelper(boss, subordinate);
   }
   public void depthFirstSearch(Employee employee) {
       employee.map();
       System.out.println(employee.firstName + " " + employee.lastName + " is a " + employee.jobTitle);
       LinkedList<Employee> neighbors = assignment.get(employee);
       if (neighbors == null)
           return;
       for (Employee e : neighbors) {
           if (!e.mapped)
               depthFirstSearch(e);
       }
   }
   public void breadthFirstSearch(Employee employee) {
       /*Check if target item is null - if so quit*/
       if (employee == null)
           return;
       /*Declare a queue using LinkedList and add the target item*/
       LinkedList<Employee> queue = new LinkedList<>();
       queue.add(employee);
       /*As long as the queue has data get the first in item .removeFirst*/
       while (!queue.isEmpty()) {
           Employee currentFirst = queue.removeFirst();
           /*If visited move on*/
           if (currentFirst.mapped) {
               continue;
           } else {
               /*else visit it and print it*/
               currentFirst.map();
               System.out.println(currentFirst.id + ": " + currentFirst.firstName + " " + currentFirst.lastName + " - " + currentFirst.jobTitle);
           }
           /*Find neighbors of the current item*/
           LinkedList<Employee> allNeighbors = assignment.get(currentFirst);
           /*If there are none move on*/
           if (allNeighbors == null)
               continue;
           /*else if there are some and not visited, add them to the queue and the process starts over*/
           for (Employee e : allNeighbors) {
               if (!e.mapped)
                   queue.add(e);
           }
       }
   }
   public void resetMapping() {
       for (Employee e : assignment.keySet()) {
           e.unmap();
       }
   }
   public static void main(String args[]) {
       Employee a = new Employee(38, "Jimbo", "Jones", "President");
       Employee b = new Employee(39, "Mike", "McCobb", "VP");
       Employee c = new Employee(43, "Elda", "Two", "VP");
       Employee d = new Employee(48, "Simon", "Sneeth", "VP");
       Employee e = new Employee(56, "Tim", "Scott", "Manager");
       Employee f = new Employee(58, "Bob", "Tilman", "Manager");
       Employee g = new Employee(78, "Andrew", "Cole", "Analyst");
       Employee h = new Employee(83, "Matt", "Lemon", "Analyst");
       Employee i = new Employee(88, "Chris", "Lock", "Analyst");
       Employee j = new Employee(71, "Agnes", "White", "Accountant");
       Employee k = new Employee(61, "George", "Mills", "Manager");
       Employee l = new Employee(59, "Thomas", "Wells", "Manager");
       Employee m = new Employee(63, "Zip", "Two", "Manager");
       Employee n = new Employee(73, "Rasta", "Him", "Technician");
       Employee o = new Employee(89, "Claire", "Collins", "Supervisor");
       Employee p = new Employee(95, "Jim", "Four", "Receptionist");
       Employee q = new Employee(104, "Tom", "Flat", "Receptionist");
       Employee r = new Employee(64, "Grace", "Marks", "Manager");
       Employee s = new Employee(68, "Dan", "Four", "Manager");
       Employee t = new Employee(117, "Shad", "Fifty", "Security");
       Employee u = new Employee(108, "Tall", "Nephew", "Legal");
       Employee v = new Employee(109, "Short", "Nephew", "Legal");


       Chart chart = new Chart(true);
       chart.addEdge(a, b);
       chart.addEdge(a, c);
       chart.addEdge(a, d);
       chart.addEdge(b, e);
       chart.addEdge(b, f);
       chart.addEdge(e, g);
       chart.addEdge(e, h);
       chart.addEdge(e, i);
       chart.addEdge(f, j);
       chart.addEdge(c, k);
       chart.addEdge(c, l);
       chart.addEdge(c, m);
       chart.addEdge(l, n);
       chart.addEdge(k, o);
       chart.addEdge(o, p);
       chart.addEdge(o, q);
       chart.addEdge(d, r);
       chart.addEdge(d, s);
       chart.addEdge(s, t);
       chart.addEdge(r, u);
       chart.addEdge(r, v);
       //System.out.println(a.firstName + " " + a.lastName + " " + a.jobTitle + " " + a.id);
       chart.resetMapping();
       //chart.depthFirstSearch(k);
       chart.resetMapping();
       chart.breadthFirstSearch(a);
   }

}