UNDIRECTED GRAPHS BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING

Size: px
Start display at page:

Download "UNDIRECTED GRAPHS BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING"

Transcription

1 BBM - ALGORIHMS DEP. O COMPUER ENGINEERING UNDIRECED GRAPHS Acknowledgement: he course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton Uniersity.

2 Undirected Graphs Graph API Depth-first search Breadth-first search Connected components Challenges ODAY

3 Undirected graphs Graph. Set of ertices connected pairwise by edges. Why study graph algorithms? housands of practical applications. Hundreds of graph algorithms known. Interesting and broadly useful abstraction. Challenging branch of computer science and discrete math.

4 Graph applications graph ertex edge communication telephone, computer fiber optic cable circuit gate, register, processor wire mechanical joint rod, beam, spring financial stock, currency transactions transportation street intersection, airport highway, airway route internet class C network connection game board position legal moe social relationship person, actor friendship, moie cast neural network neuron synapse protein network protein protein-protein interaction chemical compound molecule bond

5 Graph terminology Path. Sequence of ertices connected by edges. Cycle. Path whose first and last ertices are the same. wo ertices are connected if there is a path between them. cycle of length edge ertex path of length ertex of degree connected components

6 Some graph-processing problems Path. Is there a path between s and t? Shortest path. What is the shortest path between s and t? Cycle. Is there a cycle in the graph? Euler tour. Is there a cycle that uses each edge exactly once? Hamilton tour. Is there a cycle that uses each ertex exactly once? Connectiity. Is there a way to connect all of the ertices? MS. What is the best way to connect all of the ertices? Biconnectiity. Is there a ertex whose remoal disconnects the graph? Planarity. Can you draw the graph in the plane with no crossing edges? Graph isomorphism. Do two adjacency lists represent the same graph? Challenge. Which of these problems are easy? difficult? intractable?

7 Graph API Depth-first search Breadth-first search Connected components Challenges UNDIRECED GRAPHS

8 Graph representation Graph drawing. Proides intuition about the structure of the graph. two drawings of the same graph Caeat. Intuition can be misleading.

9 Graph representation Vertex representation. his lecture: use integers between and V. Applications: conert between names and integers with symbol table. A Anomalies. B D C E self-loop G Anomalies parallel edges symbol table

10 Graph API public class Graph Graph(int V) Graph(In in) create an empty graph with V ertices create a graph from input stream oid addedge(int, int w) add an edge -w Iterable<Integer> adj(int ) ertices adjacent to int V() int E() String tostring() number of ertices number of edges string representation In in = new In(args[]); Graph G = new Graph(in); for (int = ; < G.V(); ++) for (int w : G.adj()) StdOut.println( + "-" + w); read graph from input stream print out each edge (twice)

11 Graph API: sample client Graph input format. V tinyg.txt E Input format for G % jaa est tinyg.txt In in = new In(args[]); Graph G = new Graph(in); for (int = ; < G.V(); ++) for (int w : G.adj()) StdOut.println( + "-" + w); read graph from input stream print out each edge (twice)

12 ypical graph-processing code compute the degree of public static int degree(graph G, int ) { int degree = ; for (int w : G.adj()) degree++; return degree; } compute maximum degree public static int maxdegree(graph G) { int max = ; for (int = ; < G.V(); ++) if (degree(g, ) > max) max = degree(g, ); return max; } compute aerage degree public static double aeragedegree(graph G) { return. * G.E() / G.V(); } count self-loops public static int numberofselfloops(graph G) { int count = ; for (int = ; < G.V(); ++) for (int w : G.adj()) if ( == w) count++; return count/; // each edge counted twice }

13 Set-of-edges graph representation Maintain a list of the edges (linked list or array).

14 Adjacency-matrix graph representation Maintain a two-dimensional V-by-V boolean array; for each edge w in graph: adj[][w] = adj[w][] = true. two entries for each edge

15 Adjacency-list graph representation Maintain ertex-indexed array of lists. Bag objects adj[] representations of the same edge

16 Adjacency-list graph representation: Jaa implementation public class Graph { priate final int V; priate Bag<Integer>[] adj; public Graph(int V) { this.v = V; adj = (Bag<Integer>[]) new Bag[V]; for (int = ; < V; ++) adj[] = new Bag<Integer>(); } adjacency lists ( using Bag data type ) create empty graph with V ertices public oid addedge(int, int w) { adj[].add(w); adj[w].add(); } add edge -w (parallel edges allowed) } public Iterable<Integer> adj(int ) { return adj[]; } iterator for ertices adjacent to

17 Graph representations In practice. Use adjacency-lists representation. Algorithms based on iterating oer ertices adjacent to. Real-world graphs tend to be sparse. huge number of ertices, small aerage ertex degree sparse (E = ) dense (E = ) wo graphs (V = )

18 Graph representations In practice. Use adjacency-lists representation. Algorithms based on iterating oer ertices adjacent to. Real-world graphs tend to be sparse. huge number of ertices, small aerage ertex degree representation space add edge edge between and w? iterate oer ertices adjacent to? list of edges E E E adjacency matrix V * V adjacency lists E + V degree() degree() * disallows parallel edges

19 Graph API Depth-first search Breadth-first search Connected components Challenges UNDIRECED GRAPHS

20 Maze exploration Maze graphs. Vertex = intersection. Edge = passage. intersection passage Goal. Explore eery intersection in the maze.

21 rémaux maze exploration Algorithm. Unroll a ball of string behind you. Mark each isited intersection and each isited passage. Retrace steps when no unisited options.

22 Depth-first search Goal. Systematically search through a graph. Idea. Mimic maze exploration. DS (to isit a ertex ) Mark as isited. Recursiely isit all unmarked ertices w adjacent to. ypical applications. ind all ertices connected to a gien source ertex. ind a path between two ertices. Design challenge. How to implement?

23 Design pattern for graph processing Design pattern. Decouple graph data type from graph processing. Create a Graph object. Pass the Graph to a graph-processing routine, e.g., Paths. Query the graph-processing routine for information. public class Paths Paths(Graph G, int s) find paths in G from source s boolean haspatho(int ) is there a path from s to? Iterable<Integer> patho(int ) path from s to ; null if no such path Paths paths = new Paths(G, s); for (int = ; < G.V(); ++) if (paths.haspatho()) StdOut.println(); print all ertices connected to s

24 Depth-first search o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. V tinyg.txt E graph G

25 Depth-first search o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] edgeo[] ertices reachable from

26 Depth-first search Goal. ind all ertices connected to s (and a path). Idea. Mimic maze exploration. Algorithm. Use recursion (ball of string). Mark each isited ertex (and keep track of edge taken to isit it). Return (retrace steps) when no unisited options. Data structures. boolean[] marked to mark isited ertices. int[] edgeo to keep tree of paths. (edgeo[w] == ) means that edge -w taken to isit w for first time

27 Depth-first search public class DepthirstPaths { priate boolean[] marked; priate int[] edgeo; priate int s; marked[] = true if connected to s edgeo[] = preious ertex on path from s to public DepthirstSearch(Graph G, int s) {... dfs(g, s); } initialize data structures find ertices connected to s } priate oid dfs(graph G, int ) { marked[] = true; for (int w : G.adj()) if (!marked[w]) { dfs(g, w); edgeo[w] = ; } } recursie DS does the work

28 Depth-first search properties Proposition. DS marks all ertices connected to s in time proportional to the sum of their degrees. Pf. Correctness: - if w marked, then w connected to s (why?) - if w connected to s, then w marked (if w unmarked, then consider last edge on a path from s to w that goes from a marked ertex to an unmarked one) set of unmarked ertices source s x set of marked ertices no such edge can exist Running time: Each ertex connected to s is isited once. w

29 Depth-first search properties Proposition. After DS, can find ertices connected to s in constant time and can find a path to s (if one exists) in time proportional to its length. Pf. edgeo[] is a parent-link representation of a tree rooted at s. public boolean haspatho(int ) { return marked[]; } public Iterable<Integer> patho(int ) { if (!haspatho()) return null; Stack<Integer> path = new Stack<Integer>(); for (int x = ; x!= s; x = edgeo[x]) path.push(x); path.push(s); return path; } x path edgeo[]

30 Graph API Depth-first search Breadth-first search Connected components Challenges UNDIRECED GRAPHS

31 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. tinycg.txt V E graph G

32 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] add to queue

33 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

34 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

35 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

36 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

37 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] done

38 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

39 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

40 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

41 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

42 Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. Breadth-first search dequeue edgeo[] queue

43 Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. Breadth-first search done edgeo[] queue

44 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

45 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

46 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

47 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] done

48 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

49 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

50 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

51 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] done

52 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

53 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

54 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

55 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

56 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] done

57 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

58 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

59 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] dequeue

60 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. queue edgeo[] done

61 Breadth-first search Repeat until queue is empty: Remoe ertex from queue. Add to queue all unmarked ertices adjacent to and mark them. edgeo[] done

62 Breadth-first search Depth-first search. Put unisited ertices on a stack. Breadth-first search. Put unisited ertices on a queue. Shortest path. ind path from s to t that uses fewest number of edges. BS (from source ertex s) Put s onto a IO queue, and mark s as isited. Repeat until the queue is empty: - remoe the least recently added ertex - add each of 's unisited neighbors to the queue, and mark them as isited. Intuition. BS examines ertices in increasing distance from s.

63 Breadth-first search properties Proposition. BS computes shortest path (number of edges) from s in a connected graph in time proportional to E + V. Pf. [correctness] Queue always consists of zero or more ertices of distance k from s, followed by zero or more ertices of distance k +. Pf. [running time] Each ertex connected to s is isited once. standard drawing dist = dist = dist =

64 Breadth-first search public class BreadthirstPaths { priate boolean[] marked; priate boolean[] edgeo[]; priate final int s; } priate oid bfs(graph G, int s) { Queue<Integer> q = new Queue<Integer>(); q.enqueue(s); marked[s] = true; while (!q.isempty()) { int = q.dequeue(); for (int w : G.adj()) { if (!marked[w]) { q.enqueue(w); marked[w] = true; edgeo[w] = ; } } } }

65 Graph API Depth-first search Breadth-first search Connected components Challenges UNDIRECED GRAPHS

66 Connectiity queries Def. Vertices and w are connected if there is a path between them. Goal. Preprocess graph to answer queries: is connected to w? in constant time. public class CC CC(Graph G) Depth-first search. [next few slides] find connected components in G boolean connected(int, int w) are and w connected? int count() number of connected components int id(int ) component identifier for

67 Connected components he relation "is connected to" is an equialence relation: Reflexie: is connected to. Symmetric: if is connected to w, then w is connected to. ransitie: if connected to w and w connected to x, then connected to x. Def. A connected component is a maximal set of connected ertices. connected components id[] Remark. Gien connected components, can answer queries in constant time.

68 Connected components Def. A connected component is a maximal set of connected ertices. connected components

69 Connected components Goal. Partition ertices into connected components. Connected components Initialize all ertices as unmarked. or each unmarked ertex, run DS to identify all ertices discoered as part of the same component. V tinyg.txt E

70 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] graph G

71 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

72 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

73 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

74 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

75 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

76 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

77 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

78 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] done

79 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

80 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

81 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] done

82 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

83 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

84 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] done

85 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] done

86 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

87 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

88 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] done

89 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

90 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

91 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] done

92 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] done

93 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] connected component connected component:

94 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] check

95 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

96 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

97 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] done

98 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] done

99 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] connected component:

100 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] check

101 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

102 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

103 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

104 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

105 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

106 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] done

107 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] done

108 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

109 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] isit

110 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] done

111 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] done

112 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] connected component:

113 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] check

114 Connected components o isit a ertex : Mark ertex as isited. Recursiely isit all unmarked ertices adjacent to. marked[] cc[] done

115 inding connected components with DS public class CC { priate boolean marked[]; priate int[] id; priate int count; public CC(Graph G) { marked = new boolean[g.v()]; id = new int[g.v()]; for (int = ; < G.V(); ++) { if (!marked[]) { dfs(g, ); count++; } } } public int count() public int id(int ) priate oid dfs(graph G, int ) id[] = id of component containing number of components run DS from one ertex in each component see next slide }

116 inding connected components with DS (continued) public int count() { return count; } public int id(int ) { return id[]; } priate oid dfs(graph G, int ) { marked[] = true; id[] = count; for (int w : G.adj()) if (!marked[w]) dfs(g, w); } number of components id of component containing all ertices discoered in same call of dfs hae same id

117 Graph API Depth-first search Breadth-first search Connected components Challenges UNDIRECED GRAPHS

118 Graph-processing challenge Problem. Is a graph bipartite? How difficult? Any programmer could do it. ypical diligent algorithms student could do it. Hire an expert. Intractable. No one knows. Impossible

119 Graph-processing challenge Problem. Is a graph bipartite? How difficult? Any programmer could do it. ypical diligent algorithms student could do it. Hire an expert. Intractable. No one knows. Impossible. simple DS-based solution (see textbook)

120 Graph-processing challenge Problem. ind a cycle. How difficult? Any programmer could do it. ypical diligent algorithms student could do it. Hire an expert. Intractable. No one knows. Impossible

121 Graph-processing challenge Problem. ind a cycle. How difficult? Any programmer could do it. ypical diligent algorithms student could do it. Hire an expert. Intractable. No one knows. (see textbook) Impossible. simple DS-based solution

122 Bridges of Königsberg he Seen Bridges of Königsberg. [Leonhard Euler ] in Königsberg in Prussia, there is an island A, called the Kneiphof; the rier which surrounds it is diided into two branches... and these branches are crossed by seen bridges. Concerning these bridges, it was asked whether anyone could arrange a route in such a way that he could cross each bridge once and only once. Euler tour. Is there a (general) cycle that uses each edge exactly once? Answer. Yes iff connected and all ertices hae een degree. o find path. DS-based algorithm (see textbook).

123 Graph-processing challenge Problem. ind a cycle that uses eery edge. Assumption. Need to use each edge exactly once. How difficult? Any programmer could do it. ypical diligent algorithms student could do it. Hire an expert. Intractable. No one knows. Impossible

124 Graph-processing challenge Problem. ind a cycle that uses eery edge. Assumption. Need to use each edge exactly once. How difficult? Any programmer could do it. ypical diligent algorithms student could do it. Hire an expert. Intractable. Eulerian tour No one knows. Impossible. (classic graph-processing problem)

125 Graph-processing challenge Problem. ind a cycle that isits eery ertex exactly once. How difficult? Any programmer could do it. ypical diligent algorithms student could do it. Hire an expert. Intractable. No one knows. Impossible

126 Graph-processing challenge Problem. ind a cycle that isits eery ertex. Assumption. Need to isit each ertex exactly once. How difficult? Any programmer could do it. ypical diligent algorithms student could do it. Hire an expert. Intractable. Hamiltonian cycle No one knows. (classical NP-complete problem) Impossible

127 Graph-processing challenge Problem. Are two graphs identical except for ertex names? How difficult? Any programmer could do it. ypical diligent algorithms student could do it. Hire an expert. Intractable. No one knows. Impossible ,,,,,,

128 Graph-processing challenge Problem. Are two graphs identical except for ertex names? How difficult? Any programmer could do it. ypical diligent algorithms student could do it. Hire an expert. Intractable. No one knows. Impossible. graph isomorphism is longstanding open problem ,,,,,,

129 Graph-processing challenge Problem. Lay out a graph in the plane without crossing edges? How difficult? Any programmer could do it. ypical diligent algorithms student could do it. Hire an expert. Intractable. No one knows. Impossible

130 Graph-processing challenge Problem. Lay out a graph in the plane without crossing edges? How difficult? Any programmer could do it. ypical diligent algorithms student could do it. Hire an expert. Intractable. No one knows. Impossible. linear-time DS-based planarity algorithm discoered by arjan in s (too complicated for practitioners)

UNDIRECTED GRAPHS TODAY BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING. Undirected graphs. Graph applications

UNDIRECTED GRAPHS TODAY BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING. Undirected graphs. Graph applications BBM - ALGORIHMS ODAY DEP. O COMPUER ENGINEERING UNDIRECED GRAPHS Undirected Graphs Graph API Depth-first search Challenges Acknowledgement: he course slides are adapted from the slides prepared by R. Sedgewick

More information

UNDIRECTED GRAPHS BBM 201 DATA STRUCTURES DEPT. OF COMPUTER ENGINEERING

UNDIRECTED GRAPHS BBM 201 DATA STRUCTURES DEPT. OF COMPUTER ENGINEERING Acknowledgement: he course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton Uniersity. BBM DAA SRUCURES DEP. O COMPUER ENGINEERING UNDIRECED GRAPHS Undirected Graphs

More information

Undirected Graphs. graph API maze exploration depth-first search breadth-first search connected components challenges.

Undirected Graphs. graph API maze exploration depth-first search breadth-first search connected components challenges. Undirected graphs Graph. Set of vertices connected pairwise by edges. Undirected Graphs References: Algorithms in Java, Chapters and Why study graph algorithms? Interesting and broadly useful abstraction.

More information

Undirected Graphs. introduction Graph API maze exploration depth-first search breadth-first search connected components challenges.

Undirected Graphs. introduction Graph API maze exploration depth-first search breadth-first search connected components challenges. Undirected Graphs Graph. Set of vertices connected pairwise by edges. Undirected Graphs Why study graph algorithms? Interesting and broadly useful abstraction. Challenging branch of computer science and

More information

! Interesting and broadly useful abstraction. ! Challenging branch of computer science and discrete math. ! Hundreds of graph algorithms known.

! Interesting and broadly useful abstraction. ! Challenging branch of computer science and discrete math. ! Hundreds of graph algorithms known. Undirected Graphs Undirected Graphs Graph. Set of objects with pairwise connections. Why study graph algorithms?! Interesting and broadly useful abstraction.! Challenging branch of computer science and

More information

DIRECTED GRAPHS BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Apr. 3, 2013

DIRECTED GRAPHS BBM ALGORITHMS DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Apr. 3, 2013 BBM - ALGORIHMS DEP. O COMPUER ENGINEERING ERKU ERDEM DIRECED GRAPHS Apr., Acknowledgement: he course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton Uniersity. Directed

More information

GRAPHS (Undirected) Graph: Set of objects with pairwise connections. Why study graph algorithms?

GRAPHS (Undirected) Graph: Set of objects with pairwise connections. Why study graph algorithms? GRAPHS (Undirected) Graph: Set of objects with pairwise connections. Why study graph algorithms? Interesting and broadly useful abstraction. Challenging branch of computer science and discrete math. Hundreds

More information

Basic Graph Search. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Basic Graph Search. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton Algorithms and Data Structures, Fall Basic Graph Search Rasmus Pagh Based on slides by Kevin Wayne, Princeton Algorithms, th Edition Robert Sedgewick and Kevin Wayne Copyright Mid-term evaluation Thanks

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II DIRECTED GRAPHS Graphs slides are modified from COS 126 slides of Dr. Robert Sedgewick. 1 Directed graphs Digraph Set of vertices connected pairwise by directed

More information

DIRECTED GRAPHS BBM 201 DATA STRUCTURES DEPT. OF COMPUTER ENGINEERING

DIRECTED GRAPHS BBM 201 DATA STRUCTURES DEPT. OF COMPUTER ENGINEERING Acknowledgement: he course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University. BBM DAA SRUCURES DEP. O COMPUER ENGINEERING DIRECED GRAPHS Directed Graphs Digraph

More information

DIRECTED GRAPHS BBM ALGORITHMS TODAY DEPT. OF COMPUTER ENGINEERING. Mar. 31, Directed graphs. Road network

DIRECTED GRAPHS BBM ALGORITHMS TODAY DEPT. OF COMPUTER ENGINEERING. Mar. 31, Directed graphs. Road network BBM - ALGORIHMS ODAY DEP. O COMPUER ENGINEERING Directed Graphs Digraph API Digraph search opological sort Strong components DIRECED GRAPHS Mar., Acknowledgement: he course slides are adapted from the

More information

DIRECTED GRAPHS BBM ALGORITHMS TODAY DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Apr. 3, Directed graphs. Road network

DIRECTED GRAPHS BBM ALGORITHMS TODAY DEPT. OF COMPUTER ENGINEERING ERKUT ERDEM. Apr. 3, Directed graphs. Road network BBM - ALGORIHMS ODAY DEP. O COMPUER ENGINEERING ERKU ERDEM Directed Graphs Digraph API Digraph search opological sort Strong components DIRECED GRAPHS Apr., Acknowledgement: he course slides are adapted

More information

CS 4407 Algorithms Lecture 5: Graphs an Introduction

CS 4407 Algorithms Lecture 5: Graphs an Introduction CS 4407 Algorithms Lecture 5: Graphs an Introduction Prof. Gregory Provan Department of Computer Science University College Cork 1 Outline Motivation Importance of graphs for algorithm design applications

More information

Directed Graphs. digraph API digraph search transitive closure topological sort strong components. Directed graphs

Directed Graphs. digraph API digraph search transitive closure topological sort strong components. Directed graphs Directed graphs Digraph. Set of vertices connected pairwise by oriented edges. Directed Graphs digraph API digraph search transitive closure topological sort strong components References: Algorithms in

More information

Graph Traversals. Ric Glassey

Graph Traversals. Ric Glassey Graph Traversals Ric Glassey glassey@kth.se Overview Graph Traversals Aim: Develop alternative strategies to moving through a graph by visiting vertices and travelling along edges Maze example Depth-first

More information

Graph Algorithms. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Graph Algorithms. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico. Graph Algorithms Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto Superior Técnico May, 0 CPD (DEI / IST) Parallel and Distributed Computing 0-0-0 / Outline

More information

4.2 Directed Graphs. digraph API digraph search topological sort strong components

4.2 Directed Graphs. digraph API digraph search topological sort strong components 4.2 Directed Graphs digraph API digraph search topological sort strong components Algorithms, 4 th Edition Robert Sedgewick and Kevin Wayne Copyright 2002 2010 November 8, 2010 8:43:22 PM Directed graphs

More information

! Vertex = species. ! Edge = from prey to predator.

! Vertex = species. ! Edge = from prey to predator. irected Graphs irected Graphs igraph. Set of objects with oriented pairwise connections. x. One-way street, hyperlink. Reference: Chapter 19, Algorithms in Java, 3 rd dition, Robert Sedgewick. Robert Sedgewick

More information

Graphs. Ric Glassey

Graphs. Ric Glassey Graphs Ric Glassey glassey@kth.se Overview Graphs Aim: Introduce graphs as a fundamental data structure that can be applied in many real-world problems Applications of graphs Definition and types of graphs

More information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction digraph API digraph search topological sort strong components

More information

Algorithms: Lecture 10. Chalmers University of Technology

Algorithms: Lecture 10. Chalmers University of Technology Algorithms: Lecture 10 Chalmers University of Technology Today s Topics Basic Definitions Path, Cycle, Tree, Connectivity, etc. Graph Traversal Depth First Search Breadth First Search Testing Bipartatiness

More information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API depth-first search breadth-first search topological sort strong components

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API depth-first search breadth-first search topological sort strong components Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu introduction digraph API depth-first search breadth-first

More information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction digraph API digraph search topological sort strong components

More information

4.2 Directed Graphs. vertex of outdegree 3 and indegree 1 digraph API digraph search topological sort strong components. directed path from 0 to 2

4.2 Directed Graphs. vertex of outdegree 3 and indegree 1 digraph API digraph search topological sort strong components. directed path from 0 to 2 Directed graphs. Directed Graphs Digraph. Set of vertices connected pairwise by directed edges. vertex of outdegree and indegree 1 digraph API digraph search topological sort strong components directed

More information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API depth-first search breadth-first search topological sort strong components

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API depth-first search breadth-first search topological sort strong components Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE https://algs4.cs.princeton.edu introduction digraph API depth-first search

More information

Graph Algorithms. Andreas Klappenecker

Graph Algorithms. Andreas Klappenecker Graph Algorithms Andreas Klappenecker Graphs A graph is a set of vertices that are pairwise connected by edges. We distinguish between directed and undirected graphs. Why are we interested in graphs? Graphs

More information

COP 4531 Complexity & Analysis of Data Structures & Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms COP Complexity & Analysis of Data Structures & Algorithms Overview of Graphs Breadth irst Search, and Depth irst Search hanks to several people who contributed to these slides including Piyush Kumar and

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 5 Exploring graphs Adam Smith 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Puzzles Suppose an undirected graph G is connected.

More information

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 3 Graphs CLRS 12-13 Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 3.1 Basic Definitions and Applications Undirected Graphs Undirected graph. G = (V, E) V

More information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction digraph API digraph search topological sort strong components

More information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction digraph API digraph search topological sort strong components

More information

Algorithms 4.2 DIRECTED GRAPHS. digraph API digraph search topological sort strong components

Algorithms 4.2 DIRECTED GRAPHS. digraph API digraph search topological sort strong components 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N digraph API digraph search topological sort strong components R O B E R T S E D G E W I C K K E V I N W A Y N E Algorithms, 4 Robert Sedgewick and

More information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction digraph API digraph search topological sort strong components

More information

3. GRAPHS. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley Copyright 2013 Kevin Wayne. Last updated on Sep 8, :19 AM

3. GRAPHS. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley Copyright 2013 Kevin Wayne. Last updated on Sep 8, :19 AM 3. GRAPHS basic definitions and applications graph connectivity and graph traversal testing bipartiteness connectivity in directed graphs DAGs and topological ordering Lecture slides by Kevin Wayne Copyright

More information

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction digraph API digraph search topological sort strong components ROBERT SEDGEWICK KEVIN WAYNE Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N ROBERT SEDGEWICK KEVIN WAYNE introduction digraph API digraph search topological sort strong components

More information

implementing the breadth-first search algorithm implementing the depth-first search algorithm

implementing the breadth-first search algorithm implementing the depth-first search algorithm Graph Traversals 1 Graph Traversals representing graphs adjacency matrices and adjacency lists 2 Implementing the Breadth-First and Depth-First Search Algorithms implementing the breadth-first search algorithm

More information

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10

CS 220: Discrete Structures and their Applications. graphs zybooks chapter 10 CS 220: Discrete Structures and their Applications graphs zybooks chapter 10 directed graphs A collection of vertices and directed edges What can this represent? undirected graphs A collection of vertices

More information

Algorithms FOURTH EDITION PART II

Algorithms FOURTH EDITION PART II Algorithms FOURTH EDITION PART II This page intentionally left blank Algorithms FOURTH EDITION PART II Robert Sedgewick and Kevin Wayne Princeton University Upper Saddle River, NJ Boston Indianapolis San

More information

Algorithms: Graphs. Amotz Bar-Noy. Spring 2012 CUNY. Amotz Bar-Noy (CUNY) Graphs Spring / 95

Algorithms: Graphs. Amotz Bar-Noy. Spring 2012 CUNY. Amotz Bar-Noy (CUNY) Graphs Spring / 95 Algorithms: Graphs Amotz Bar-Noy CUNY Spring 2012 Amotz Bar-Noy (CUNY) Graphs Spring 2012 1 / 95 Graphs Definition: A graph is a collection of edges and vertices. Each edge connects two vertices. Amotz

More information

3.1 Basic Definitions and Applications

3.1 Basic Definitions and Applications Graphs hapter hapter Graphs. Basic efinitions and Applications Graph. G = (V, ) n V = nodes. n = edges between pairs of nodes. n aptures pairwise relationship between objects: Undirected graph represents

More information

CS2 Algorithms and Data Structures Note 9

CS2 Algorithms and Data Structures Note 9 CS2 Algorithms and Data Structures Note 9 Graphs The remaining three lectures of the Algorithms and Data Structures thread will be devoted to graph algorithms. 9.1 Directed and Undirected Graphs A graph

More information

Konigsberg Bridge Problem

Konigsberg Bridge Problem Graphs Konigsberg Bridge Problem c C d g A Kneiphof e D a B b f c A C d e g D a b f B Euler s Graph Degree of a vertex: the number of edges incident to it Euler showed that there is a walk starting at

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 4 Graphs Definitions Traversals Adam Smith 9/8/10 Exercise How can you simulate an array with two unbounded stacks and a small amount of memory? (Hint: think of a

More information

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications

3.1 Basic Definitions and Applications. Chapter 3. Graphs. Undirected Graphs. Some Graph Applications Chapter 3 31 Basic Definitions and Applications Graphs Slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley All rights reserved 1 Undirected Graphs Some Graph Applications Undirected graph G = (V,

More information

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. Chapter 3 Graphs Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 3.1 Basic Definitions and Applications Undirected Graphs Undirected graph. G = (V, E) V = nodes. E

More information

Graph theory: basic concepts

Graph theory: basic concepts Graph theory: basic concepts Graphs and networks allow modelling many different decision problems Graphs permit to represent relationships among different kinds of entities: Cities connected by roads Computers

More information

Understand graph terminology Implement graphs using

Understand graph terminology Implement graphs using raphs Understand graph terminology Implement graphs using djacency lists and djacency matrices Perform graph searches Depth first search Breadth first search Perform shortest-path algorithms Disjkstra

More information

Breadth First Search. cse2011 section 13.3 of textbook

Breadth First Search. cse2011 section 13.3 of textbook Breadth irst Search cse section. of textbook Graph raversal (.) Application example Given a graph representation and a vertex s in the graph, find all paths from s to the other vertices. wo common graph

More information

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. Lecture 9 Graphs This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. You need to be familiar with the design and use of basic data structures such as Lists, Stacks,

More information

Directed Graphs. digraph API digraph search transitive closure topological sort strong components. Directed graphs

Directed Graphs. digraph API digraph search transitive closure topological sort strong components. Directed graphs irected graphs igraph. Set of vertices connected pairwise by oriented edges. irected raphs digraph AP digraph search transitive closure topological sort strong components References: Algorithms in Java,

More information

Computational Optimization ISE 407. Lecture 19. Dr. Ted Ralphs

Computational Optimization ISE 407. Lecture 19. Dr. Ted Ralphs Computational Optimization ISE 407 Lecture 19 Dr. Ted Ralphs ISE 407 Lecture 19 1 Search Algorithms Search algorithms are fundamental techniques applied to solve a wide range of optimization problems.

More information

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms CSE 373: Data Structures and Algorithms Lecture 15: Graph Data Structures, Topological Sort, and Traversals (DFS, BFS) Instructor: Lilian de Greef Quarter: Summer 2017 Today: Announcements Graph data structures

More information

Copyright 2000, Kevin Wayne 1

Copyright 2000, Kevin Wayne 1 Chapter 3 - Graphs Undirected Graphs Undirected graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise relationship between objects. Graph size parameters: n = V, m = E. Directed

More information

6.2. Paths and Cycles

6.2. Paths and Cycles 6.2. PATHS AND CYCLES 85 6.2. Paths and Cycles 6.2.1. Paths. A path from v 0 to v n of length n is a sequence of n+1 vertices (v k ) and n edges (e k ) of the form v 0, e 1, v 1, e 2, v 2,..., e n, v n,

More information

CSI 604 Elementary Graph Algorithms

CSI 604 Elementary Graph Algorithms CSI 604 Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. (Second edition) 1 / 25 Graphs: Basic Definitions Undirected Graph G(V, E): V is set of nodes (or vertices) and E is the

More information

DS UNIT 4. Matoshri College of Engineering and Research Center Nasik Department of Computer Engineering Discrete Structutre UNIT - IV

DS UNIT 4. Matoshri College of Engineering and Research Center Nasik Department of Computer Engineering Discrete Structutre UNIT - IV Sr.No. Question Option A Option B Option C Option D 1 2 3 4 5 6 Class : S.E.Comp Which one of the following is the example of non linear data structure Let A be an adjacency matrix of a graph G. The ij

More information

6.4 Maximum Flow. overview Ford-Fulkerson implementations applications

6.4 Maximum Flow. overview Ford-Fulkerson implementations applications 6.4 Maximum Flow overview Ford-Fulkerson implementations applications Algorithms, 4 th Edition Robert Sedgewick and Kevin Wayne Copyright 2002 2010 April 18, 2011 11:02:27 AM Mincut problem Given. A weighted

More information

3.1 Basic Definitions and Applications

3.1 Basic Definitions and Applications Chapter 3 Graphs Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved. 1 3.1 Basic Definitions and Applications Undirected Graphs Undirected graph. G = (V, E) V = nodes. E

More information

Algorithms 4.2 DIRECTED GRAPHS. digraph API digraph search topological sort strong components

Algorithms 4.2 DIRECTED GRAPHS. digraph API digraph search topological sort strong components 4.2 DIRECTED GRAPHS Algorithms F O U R T H E D I T I O N digraph API digraph search topological sort strong components R O B E R T S E D G E W I C K K E V I N W A Y N E Algorithms, 4 th Edition Robert

More information

Lecture 3: Graphs and flows

Lecture 3: Graphs and flows Chapter 3 Lecture 3: Graphs and flows Graphs: a useful combinatorial structure. Definitions: graph, directed and undirected graph, edge as ordered pair, path, cycle, connected graph, strongly connected

More information

Algorithms COMBINATORIAL SEARCH. permutations backtracking counting subsets paths in a graph

Algorithms COMBINATORIAL SEARCH. permutations backtracking counting subsets paths in a graph COMBINATORIAL SEARCH Algorithms F O U R T H E D I T I O N permutations backtracking counting subsets paths in a graph R O B E R T S E D G E W I C K K E V I N W A Y N E Algorithms, 4 th Edition Robert Sedgewick

More information

Graph: representation and traversal

Graph: representation and traversal Graph: representation and traversal CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang! Acknowledgement The set of slides have use materials from the following resources Slides for textbook

More information

Crossing bridges. Crossing bridges Great Ideas in Theoretical Computer Science. Lecture 12: Graphs I: The Basics. Königsberg (Prussia)

Crossing bridges. Crossing bridges Great Ideas in Theoretical Computer Science. Lecture 12: Graphs I: The Basics. Königsberg (Prussia) 15-251 Great Ideas in Theoretical Computer Science Lecture 12: Graphs I: The Basics February 22nd, 2018 Crossing bridges Königsberg (Prussia) Now Kaliningrad (Russia) Is there a way to walk through the

More information

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs 2011 Pearson Addison-Wesley. All rights reserved 14 A-1 Terminology G = {V, E} A graph G consists of two sets A set V of vertices, or nodes A set E of edges A subgraph Consists of a subset

More information

Combinatorial Search. permutations backtracking counting subsets paths in a graph. Overview

Combinatorial Search. permutations backtracking counting subsets paths in a graph. Overview Overview Exhaustive search. Iterate through all elements of a search space. Combinatorial Search Backtracking. Systematic method for examining feasible solutions to a problem, by systematically eliminating

More information

Algorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction. digraph API digraph search topological sort

Algorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction. digraph API digraph search topological sort Algorithms ROBERT SEDGEWICK KEVIN WAYNE. DIRECTED GRAPHS. DIRECTED GRAPHS introduction introduction Algorithms F O U R T H E D I T I O N digraph API digraph search topological sort Algorithms digraph API

More information

Algorithms. Graphs. Algorithms

Algorithms. Graphs. Algorithms Algorithms Graphs Algorithms Graphs Definition: A graph is a collection of edges and vertices. Each edge connects two vertices. Algorithms 1 Graphs Vertices: Nodes, points, computers, users, items,...

More information

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3 Multidimensional Arrays & Graphs CMSC 420: Lecture 3 Mini-Review Abstract Data Types: List Stack Queue Deque Dictionary Set Implementations: Linked Lists Circularly linked lists Doubly linked lists XOR

More information

Graph Algorithms. A Brief Introduction. 高晓沨 (Xiaofeng Gao) Department of Computer Science Shanghai Jiao Tong Univ.

Graph Algorithms. A Brief Introduction. 高晓沨 (Xiaofeng Gao) Department of Computer Science Shanghai Jiao Tong Univ. Graph Algorithms A Brief Introduction 高晓沨 (Xiaofeng Gao) Department of Computer Science Shanghai Jiao Tong Univ. 目录 2015/5/7 1 Graph and Its Applications 2 Introduction to Graph Algorithms 3 References

More information

CSE 373 Final Exam 3/14/06 Sample Solution

CSE 373 Final Exam 3/14/06 Sample Solution Question 1. (6 points) A priority queue is a data structure that supports storing a set of values, each of which has an associated key. Each key-value pair is an entry in the priority queue. The basic

More information

To list all vertices connected to a vertex u in graph G we can use the following loop:

To list all vertices connected to a vertex u in graph G we can use the following loop: Using nextdjacent() To list all vertices connected to a vertex u in graph we can use the following loop: for (int v=.nextdjacent(u,-1); v>=0; v=.nextdjacent(u,v)) { System.out.print(v); djacency Lists

More information

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11 Chapter 3 - Graphs Undirected Graphs Undirected graph. G = (V, E) V = nodes. E = edges between pairs of nodes. Captures pairwise relationship between objects. Graph size parameters: n = V, m = E. V = {

More information

Data Structure Lecture#23: Graphs (Chapter 11) U Kang Seoul National University

Data Structure Lecture#23: Graphs (Chapter 11) U Kang Seoul National University Data Structure Lecture#23: Graphs (Chapter 11) U Kang Seoul National University U Kang 1 In This Lecture Basic terms and definitions of graphs How to represent graphs Graph traversal methods U Kang 2 Graphs

More information

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms

Breadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms Breadth irst Search CSE Winter Graph raversal Application examples Given a graph representation and a vertex s in the graph ind all paths from s to the other vertices wo common graph traversal algorithms

More information

Introduction III. Graphs. Motivations I. Introduction IV

Introduction III. Graphs. Motivations I. Introduction IV Introduction I Graphs Computer Science & Engineering 235: Discrete Mathematics Christopher M. Bourke cbourke@cse.unl.edu Graph theory was introduced in the 18th century by Leonhard Euler via the Königsberg

More information

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph: Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. Representing a graph: Graph G(V, E): V set of nodes (vertices); E set of edges. Notation: n = V and m = E. (Vertices are numbered

More information

Algorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction. digraph API digraph search topological sort

Algorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction. digraph API digraph search topological sort Algorithms ROBRT SDGWICK KVIN WAYN. DIRCTD GRAPHS. DIRCTD GRAPHS introduction introduction Algorithms F O U R T H D I T I O N digraph API digraph search topological sort Algorithms digraph API digraph

More information

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1 Graph Algorithms Chapter 22 CPTR 430 Algorithms Graph Algorithms Why Study Graph Algorithms? Mathematical graphs seem to be relatively specialized and abstract Why spend so much time and effort on algorithms

More information

Algorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction digraph API. digraph API. digraph search topological sort

Algorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction digraph API. digraph API. digraph search topological sort Algorithms ROBERT SEDGEWICK KEVIN WAYNE. DIRECTED GRAPHS. DIRECTED GRAPHS introduction introduction digraph API digraph API Algorithms F O U R T H E D I T I O N digraph search topological sort Algorithms

More information

Graphs and Genetics. Outline. Computational Biology IST. Ana Teresa Freitas 2015/2016. Slides source: AED (MEEC/IST); Jones and Pevzner (book)

Graphs and Genetics. Outline. Computational Biology IST. Ana Teresa Freitas 2015/2016. Slides source: AED (MEEC/IST); Jones and Pevzner (book) raphs and enetics Computational Biology IST Ana Teresa Freitas / Slides source: AED (MEEC/IST); Jones and Pevzner (book) Outline l Motivacion l Introduction to raph Theory l Eulerian & Hamiltonian Cycle

More information

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1 CS200: Graphs Prichard Ch. 14 Rosen Ch. 10 CS200 - Graphs 1 Graphs A collection of nodes and edges What can this represent? n A computer network n Abstraction of a map n Social network CS200 - Graphs 2

More information

Algorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction digraph API. digraph API. digraph search topological sort

Algorithms. Algorithms. Algorithms 4.2 DIRECTED GRAPHS. introduction. introduction digraph API. digraph API. digraph search topological sort Algorithms ROBERT SEDGEWICK KEVIN WAYNE. DIRECTED GRAPHS. DIRECTED GRAPHS introduction introduction digraph API digraph API Algorithms F O U R T H E D I T I O N digraph search topological sort Algorithms

More information

Graphs. Data Structures 1 Graphs

Graphs. Data Structures 1 Graphs Graphs Graph Applications Definitions Graph Representations(adjacency matrix/list) Graph ADT Graph Traversals: BFS, DFS Shortest Path Algorithms Minimum Spanning Tree Algorithms Data Structures Graphs

More information

Graphs. The ultimate data structure. graphs 1

Graphs. The ultimate data structure. graphs 1 Graphs The ultimate data structure graphs 1 Definition of graph Non-linear data structure consisting of nodes & links between them (like trees in this sense) Unlike trees, graph nodes may be completely

More information

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V.

I A graph is a mathematical structure consisting of a set of. I Formally: G =(V, E), where V is a set and E V V. Directed and Undirected Graphs Inf 2B: Graphs, BFS, DFS Kyriakos Kalorkoti School of Informatics University of Edinburgh I A graph is a mathematical structure consisting of a set of vertices and a set

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 3 Data Structures Graphs Traversals Strongly connected components Sofya Raskhodnikova L3.1 Measuring Running Time Focus on scalability: parameterize the running time

More information

COT 6405 Introduction to Theory of Algorithms

COT 6405 Introduction to Theory of Algorithms COT 6405 Introduction to Theory of Algorithms Topic 14. Graph Algorithms 11/7/2016 1 Elementary Graph Algorithms How to represent a graph? Adjacency lists Adjacency matrix How to search a graph? Breadth-first

More information

Inf 2B: Graphs, BFS, DFS

Inf 2B: Graphs, BFS, DFS Inf 2B: Graphs, BFS, DFS Kyriakos Kalorkoti School of Informatics University of Edinburgh Directed and Undirected Graphs I A graph is a mathematical structure consisting of a set of vertices and a set

More information

Math 776 Graph Theory Lecture Note 1 Basic concepts

Math 776 Graph Theory Lecture Note 1 Basic concepts Math 776 Graph Theory Lecture Note 1 Basic concepts Lectured by Lincoln Lu Transcribed by Lincoln Lu Graph theory was founded by the great Swiss mathematician Leonhard Euler (1707-178) after he solved

More information

GRAPHICAL ALGORITHMS. UNIT _II Lecture-12 Slides No. 3-7 Lecture Slides No Lecture Slides No

GRAPHICAL ALGORITHMS. UNIT _II Lecture-12 Slides No. 3-7 Lecture Slides No Lecture Slides No GRAPHICAL ALGORITHMS UNIT _II Lecture-12 Slides No. 3-7 Lecture-13-16 Slides No. 8-26 Lecture-17-19 Slides No. 27-42 Topics Covered Graphs & Trees ( Some Basic Terminologies) Spanning Trees (BFS & DFS)

More information

BIL694-Lecture 1: Introduction to Graphs

BIL694-Lecture 1: Introduction to Graphs BIL694-Lecture 1: Introduction to Graphs Lecturer: Lale Özkahya Resources for the presentation: http://www.math.ucsd.edu/ gptesler/184a/calendar.html http://www.inf.ed.ac.uk/teaching/courses/dmmr/ Outline

More information

Algorithms. Algorithms 4.4 SHORTEST PATHS. APIs properties Bellman Ford algorithm Dijkstra s algorithm ROBERT SEDGEWICK KEVIN WAYNE

Algorithms. Algorithms 4.4 SHORTEST PATHS. APIs properties Bellman Ford algorithm Dijkstra s algorithm ROBERT SEDGEWICK KEVIN WAYNE Algorithms ROBERT SEDGEWICK KEVIN WAYNE 4.4 SHORTEST PATHS Algorithms F O U R T H E D I T I O N APIs properties Bellman Ford algorithm Dijkstra s algorithm ROBERT SEDGEWICK KEVIN WAYNE https://algs4.cs.princeton.edu

More information

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions Introduction Chapter 9 Graph Algorithms graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 2 Definitions an undirected graph G = (V, E) is

More information

Graphs (MTAT , 6 EAP) Lectures: Mon 14-16, hall 404 Exercises: Wed 14-16, hall 402

Graphs (MTAT , 6 EAP) Lectures: Mon 14-16, hall 404 Exercises: Wed 14-16, hall 402 Graphs (MTAT.05.080, 6 EAP) Lectures: Mon 14-16, hall 404 Exercises: Wed 14-16, hall 402 homepage: http://courses.cs.ut.ee/2012/graafid (contains slides) For grade: Homework + three tests (during or after

More information

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms Chapter 9 Graph Algorithms 2 Introduction graph theory useful in practice represent many real-life problems can be slow if not careful with data structures 3 Definitions an undirected graph G = (V, E)

More information

ECE4050 Data Structures and Algorithms. Lecture 8: Graphs

ECE4050 Data Structures and Algorithms. Lecture 8: Graphs ECE4050 Data Structures and Algorithms Lecture 8: Graphs 1 Graphs A graph G = (V, E) consists of a set of vertices V, and a set of edges E, such that each edge in E is a connection between a pair of vertices

More information

GRAPHS, GRAPH MODELS, GRAPH TERMINOLOGY, AND SPECIAL TYPES OF GRAPHS

GRAPHS, GRAPH MODELS, GRAPH TERMINOLOGY, AND SPECIAL TYPES OF GRAPHS GRAPHS, GRAPH MODELS, GRAPH TERMINOLOGY, AND SPECIAL TYPES OF GRAPHS DR. ANDREW SCHWARTZ, PH.D. 10.1 Graphs and Graph Models (1) A graph G = (V, E) consists of V, a nonempty set of vertices (or nodes)

More information

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1 Undirected Graphs Terminology. Free Trees. Representations. Minimum Spanning Trees (algorithms: Prim, Kruskal). Graph Traversals (dfs, bfs). Articulation points & Biconnected Components. Graph Matching

More information

Graph Theory. 1 Introduction to Graphs. Martin Stynes Department of Mathematics, UCC January 26, 2011

Graph Theory. 1 Introduction to Graphs. Martin Stynes Department of Mathematics, UCC   January 26, 2011 Graph Theory Martin Stynes Department of Mathematics, UCC email: m.stynes@ucc.ie January 26, 2011 1 Introduction to Graphs 1 A graph G = (V, E) is a non-empty set of nodes or vertices V and a (possibly

More information

4.4 Shortest Paths. Dijkstra's algorithm implementation acyclic networks negative weights. Reference: Algorithms in Java, 4 th edition, Section 4.

4.4 Shortest Paths. Dijkstra's algorithm implementation acyclic networks negative weights. Reference: Algorithms in Java, 4 th edition, Section 4. 4.4 Shortest Paths Dijkstra's algorithm implementation acyclic networks negative weights Reference: Algorithms in Java, 4 th edition, Section 4.4 Algorithms in Java, 4 th Edition Robert Sedgewick and Kevin

More information