Algorithmen und Datenstrukturen II Peter Steffen AG Praktische Informatik Technische Fakultät Universität Bielefeld Vorlesung Sommer 2009 Peter Steffen A&D II, Vorlesung 2009 Universität Bielefeld Teil I Implementierung von Graphen Peter Steffen A&D II, Vorlesung 2009 Universität Bielefeld Die Klasse Edge public class Edge { int v; int w; Edge(int v, int w) { this.v = v; this.w = w; } } Peter Steffen A&D II, Vorlesung 2009 Universität Bielefeld Die Klasse Graph import java.util.LinkedList; public class Graph { private private private private int vertices; int edges; boolean directed; LinkedList<Integer>[] adjlists; public Graph(int v, boolean d){ vertices = v; directed = d; edges = 0; adjlists = new LinkedList[v]; for (int i=0; i<v; i++) adjlists[i] = new LinkedList<Integer>(); } int numOfV(){ return vertices; } int numOfE(){ return edges; } boolean directed(){ return directed; } Peter Steffen A&D II, Vorlesung 2009 Universität Bielefeld Die Klasse Graph void insert(Edge e){ adjlists[e.v].add(new Integer(e.w)); edges++; if (!directed) { adjlists[e.w].add(new Integer(e.v)); edges++; } } void remove(Edge e){ adjlists[e.v].remove(new Integer(e.w)); edges--; if (!directed) { adjlists[e.w].remove(new Integer(e.v)); edges--; } } boolean edge(int v, int w){ return adjlists[v].contains(w); } LinkedList<Integer> getAdjList(int v){ return adjlists[v]; } } Peter Steffen A&D II, Vorlesung 2009 Universität Bielefeld Ein kleiner Test import java.util.LinkedList; public class GraphTest{ public static void main(String[] args){ Graph g = new Graph(8, false); g.insert(new Edge(0,4)); g.insert(new Edge(0,1)); g.insert(new Edge(1,5)); g.insert(new Edge(5,2)); g.insert(new Edge(5,6)); g.insert(new Edge(2,3)); g.insert(new Edge(6,2)); g.insert(new Edge(6,3)); g.insert(new Edge(3,7)); g.insert(new Edge(6,7)); for (int i=0; i<g.numOfV(); i++){ System.out.println("Vertice " + i + ":"); LinkedList<Integer> l = g.getAdjList(i); for (int j=0; j<l.size(); j++) System.out.println("Edge to: " + l.get(j).intValue()); } } } Peter Steffen A&D II, Vorlesung 2009 Universität Bielefeld Breitensuche import java.util.LinkedList; class BFS{ public static void bfs(Graph g, int s){ final char WHITE = 0; final char GRAY = 1; final char BLACK = 2; char[] color = new char[g.numOfV()]; int[] d = new int[g.numOfV()]; int[] pi = new int[g.numOfV()]; for (int u=0; u<g.numOfV(); u++){ color[u] = WHITE; d[u] = -1; pi[u] = -1; } color[s] = GRAY; d[s] = 0; pi[s] = -1; Peter Steffen A&D II, Vorlesung 2009 Universität Bielefeld Breitensuche LinkedList<Integer> q = new LinkedList<Integer>(); q.add(new Integer(s)); while (!q.isEmpty()){ int u = q.remove().intValue(); LinkedList<Integer> l = g.getAdjList(u); for (int i = 0; i<l.size(); i++){ int v = l.get(i).intValue(); if (color[v] == WHITE){ color[v] = GRAY; d[v] = d[u] + 1; pi[v] = u; q.add(new Integer(v)); } } color[u] = BLACK; System.out.println("finished with node " + u); } System.out.println("Distances from node " + s + ":"); for(int i = 0; i<g.numOfV(); i++) System.out.println("Node " + i + ": " + d[i]); } } Peter Steffen A&D II, Vorlesung 2009 Universität Bielefeld Tiefensuche import java.util.LinkedList; class DFS{ static static static static static static static final char WHITE = 0; final char GRAY = 1; final char BLACK = 2; char[] color; int[] pi; int[] d, f; int time; public static void dfs(Graph g){ color = new char[g.numOfV()]; pi = new int[g.numOfV()]; d = new int[g.numOfV()]; f = new int[g.numOfV()]; for(int u=0; u<g.numOfV(); u++){ color[u] = WHITE; pi[u] = -1; } time = 0; for(int u=0; u<g.numOfV(); u++){ if (color[u] == WHITE) dfs_visit(g, u); } } Peter Steffen A&D II, Vorlesung 2009 Universität Bielefeld Tiefensuche private static void dfs_visit(Graph g, int u){ color[u] = GRAY; time++; d[u] = time; LinkedList<Integer> l = g.getAdjList(u); for (int i = 0; i<l.size(); i++){ int v = l.get(i).intValue(); if (color[v] == WHITE){ pi[v] = u; dfs_visit(g, v); } } color[u] = BLACK; f[u] = ++time; System.out.println("finished with node " + u); System.out.println("d = " + d[u] + ", f = " + f[u]); } } Peter Steffen A&D II, Vorlesung 2009 Universität Bielefeld K. Arnold, J. Gosling: JavaTM - Die Programmiersprache. Addison-Wesley, 1996. T.H. Cormen, C.E. Leierson, R.L. Rivest: Introduction to Algorithms. MIT Press, 1990. D. Flanagan: Java in a Nutshell. O’Reilly & Associates Inc., 1996. F. Jobst: Programmieren in Java. Hanser Verlag, 1996. H. Klaeren: Vom Problem zum Programm. 2.Auflage, B.G. Teubner Verlag, 1991. K. Echtle, M. Goedicke: Lehrbuch der Programmierung mit Java. dpunkt-Verlag, 2000. Peter Steffen A&D II, Vorlesung 2009 Universität Bielefeld