220 likes | 391 Views
Graph. Algorithms Baojian Hua huabj@mail.ustc.edu.cn May 26, 2008. Graph. Graph is a basic and simple mathematical abstraction with a long history Date back at least to Euler Focus on topological structure (geometry?) With many important applications Map, scheduling, transaction, …
E N D
Graph Algorithms Baojian Hua huabj@mail.ustc.edu.cn May 26, 2008
Graph • Graph is a basic and simple mathematical abstraction with a long history • Date back at least to Euler • Focus on topological structure (geometry?) • With many important applications • Map, scheduling, transaction, … • In computer science: • Network, compiler, web, circuit, …
name name succs succs next next node next /\ node next Adjacency List // a simplified representation (no explicit edges) class Node { String name; Successors succs; Node next; } class Successors { Node node; Successors next; }
name edges next from to next /\ Or With Edges class Node { String name; Edges edges; Node next; } class Edges { Node from; Node to; Edegs next; }
Depth-first Search (DFS) // a much simplified version dfs (Node n){ visited [n] = true; // memoization :-) for (each successors t of n){ if (visited[t]) {} else dfs (t); } } // with initial call dfs (start).
Example B A D C Initial start node: A
Example B A D C Visited A
Example B A D C Visited B
Example B A D C Visited C
Example B A D C Visited D
Example B A D C All successors of D has been visited. Go back from D.
Example B A D C All successors of C has been visited. Go back from C.
Example B A D C All successors of B has been visited. Go back from B.
Example B A D C All successors of A has been visited. Go back from A.
DFS Tree B A D C
DFS Tree B A D C
DFS Tree B A D C
Problems #1 • Simple Path: is there a simple path from node u to v in a digraph g? • Or is u and v connected?
Problems #2 • Simple connectivity: is a given undirected graph connected? • Or how many nodes are there in a connected component?
Problems #3 • Cycle detection: is there a cycle in a digraph?
Problems #4 • Two colorability: is it possible to color all the nodes in a digraph g such that no adjacent nodes with the same color? • Or: is there a cycle with odd length?