Graphs. Introduction Graphs are a collection of vertices and edges Graphs are a collection of vertices and edges The solid circles are the vertices A,

Slides:



Advertisements
Similar presentations
Decision Maths Networks Kruskals Algorithm Wiltshire Networks A Network is a weighted graph, which just means there is a number associated with each.
Advertisements

Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 27 Graph Applications.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Graph & BFS.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
Graphs Chapter 12.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
Weighted Graphs Computing 2 COMP s1 Sedgewick Part 5: Chapter
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Graphs are collections of vertices and edges. Vertices are simple objects with associated names and other properties. Edges are connections between.
Depth-First Search Lecture 21: Graph Traversals
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs. Introduction Graphs are a collection of vertices and edges Graphs are a collection of vertices and edges The solid circles are the vertices A,
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 22 Graphs and Applications.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
May 12th – Minimum Spanning Trees
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Spanning Trees.
Graphs & Graph Algorithms 2
Graphs Chapter 13.
CSE 373 Data Structures and Algorithms
Graphs.
Graphs Part 2 Adjacency Matrix
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Networks Kruskal’s Algorithm
Graphs.
Chapter 9 Graph algorithms
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Graphs

Introduction Graphs are a collection of vertices and edges Graphs are a collection of vertices and edges The solid circles are the vertices A, B, C and D The solid circles are the vertices A, B, C and D The lines are the edges The lines are the edges

Weighted Graphs We can also assign values to the edges (a cost related to connecting the two vertices together) and form a weighted graph We can also assign values to the edges (a cost related to connecting the two vertices together) and form a weighted graph

Directed Graph There’s also a directed graph (sometimes called a digraph) that makes the edges one- way There’s also a directed graph (sometimes called a digraph) that makes the edges one- way Since edges are only one-way, b has an edge to x, but x does not have an edge to b Since edges are only one-way, b has an edge to x, but x does not have an edge to b As you can probably guess, you can have a weighted, directed graph as well As you can probably guess, you can have a weighted, directed graph as well

Terminology: Connected A graph is said to be connected if there is a path from every vertex to every other vertex in the graph A graph is said to be connected if there is a path from every vertex to every other vertex in the graph If a graph is not connected, then we call it disconnected If a graph is not connected, then we call it disconnected

Terminology: Path A path is a list of vertices in which successive vertices are connected by edges in the graph. In this graph, one example path is A path is a list of vertices in which successive vertices are connected by edges in the graph. In this graph, one example path is A  B  D  A  C A  B  D  A  C A simple path is a path in which no vertex is repeated. A simple path is a path in which no vertex is repeated. Hence, A  B  D  A  C is not a simple path, but A  B  D  C is Hence, A  B  D  A  C is not a simple path, but A  B  D  C is

Terminology: Cycle A cycle is a path that is simple except that the first and last vertices are the same A cycle is a path that is simple except that the first and last vertices are the same In this graph, the path 7  5  6  8  7 is a cycle In this graph, the path 7  5  6  8  7 is a cycle

Terminology: Connected Component A connected component or component is a maximal connected subgraph A connected component or component is a maximal connected subgraph

Graph Representation Adjacency Matrix Adjacency Matrix Adjacency List Adjacency List Edge List Edge List Adjacency Function Adjacency Function

Adjacency Matrix An adjacency matrix is simply an N x N matrix (where N is the number of vertices in the graph). An adjacency matrix is simply an N x N matrix (where N is the number of vertices in the graph). Can store boolean (is there an edge?) Can store boolean (is there an edge?) Can store number (how many edges connect nodes) Can store number (how many edges connect nodes) Can store weights (for weighted graph) Can store weights (for weighted graph)

Adjacency List The adjacency list simply keeps a linked list for each vertex that contains the vertices that are neighbors to it (that is, the list of vertices such that there exists an edge from the subject node to each of the vertices) The adjacency list simply keeps a linked list for each vertex that contains the vertices that are neighbors to it (that is, the list of vertices such that there exists an edge from the subject node to each of the vertices) A : B  C  D A : B  C  D B : A  D B : A  D C : A  D C : A  D D : A  B  C D : A  B  C

Motivation Graphs are very useful for representing a large array of problems and for solving everyday “real world” problems, on which the contest loves to focus. Graphs are very useful for representing a large array of problems and for solving everyday “real world” problems, on which the contest loves to focus. Map intersections to vertices and roads to edges, and now you have a representation to help you route fire trucks to fires. Map intersections to vertices and roads to edges, and now you have a representation to help you route fire trucks to fires. Map a network of computers and routers to vertices, physical connections to edges, and you have a way to find the minimum number of hops between two given computers. Map a network of computers and routers to vertices, physical connections to edges, and you have a way to find the minimum number of hops between two given computers. Map a set of rooms that need water to vertices, all combinations of distances between pairs of rooms to the edges, and then you can determine the smallest amount of pipe needed to connect all the rooms together. Map a set of rooms that need water to vertices, all combinations of distances between pairs of rooms to the edges, and then you can determine the smallest amount of pipe needed to connect all the rooms together. Map the buildings on the UCF campus to vertices, the sidewalks between the buildings to vertices, and then you’ll be able to find the shortest walking distance from the Computer Science Building to any other building. Map the buildings on the UCF campus to vertices, the sidewalks between the buildings to vertices, and then you’ll be able to find the shortest walking distance from the Computer Science Building to any other building. Many, many others! Many, many others!

Testing

Algorithms DFS DFS BFS BFS Two Coloring Two Coloring Floyd-Warshall’s Algorithm (All Pairs Shortest Path) Floyd-Warshall’s Algorithm (All Pairs Shortest Path) Kruskal’s Algorithm (Minimum Spanning Tree) Kruskal’s Algorithm (Minimum Spanning Tree)

Depth First Search The name “Depth-First Search” comes from the fact that you search as deep as you can in the graph first The name “Depth-First Search” comes from the fact that you search as deep as you can in the graph first We need an adjacency matrix and an array to keep track of whether we visited a given node yet We need an adjacency matrix and an array to keep track of whether we visited a given node yet adj:array[1..N, 1..N] of Boolean visited:array[1..N] of Boolean (initialized to all false) DFS(node) { visited[node] = true; if (node is target node) then process accordingly for I = 1 to N { if (adj[node][I]) and (not visited[I]) then DFS(I); }

DFS Motivation Most basic searching algorithm Most basic searching algorithm Mazes Mazes Path finding Path finding Cycle detection Cycle detection

DFS Example DB A C ED B A C ED B A C E discovery edge back edge A visited vertex A unexplored vertex unexplored edge

DFS Example DB A C E DB A C E DB A C E D B A C E

Breadth First Search The name “Breadth-First Search” comes from the idea that you search as wide as possible at each step The name “Breadth-First Search” comes from the idea that you search as wide as possible at each step Again, we need the adjacency list and visited array. We will also need a queue data structure Again, we need the adjacency list and visited array. We will also need a queue data structure adj:array[1..N, 1..N] of Boolean visited:array[1..N] of Boolean (initialized to all false) BFS(node) { enqueue(node) visited[node] = true while (queue is not empty) { t = dequeue() for I = 1 to N { if (adj[t][I]) and (not visited[I]) then enqueue(I) visited[I] = true }

BFS Motivation Finding all connected components in a graph Finding all connected components in a graph Finding the shortest path between two nodes u and v (in an unweighted graph) Finding the shortest path between two nodes u and v (in an unweighted graph) Testing a graph for bipartiteness Testing a graph for bipartiteness

Example C B A E D discovery edge cross edge A visited vertex A unexplored vertex unexplored edge L0L0 L1L1 F CB A E D L0L0 L1L1 F CB A E D L0L0 L1L1 F

Example (cont.) CB A E D L0L0 L1L1 F CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2

CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2 CB A E D L0L0 L1L1 F L2L2

Graph Coloring Problem Goal: Assign colors to each vertex in a graph such that no two vertices that are connected by an edge are the same color. Goal: Assign colors to each vertex in a graph such that no two vertices that are connected by an edge are the same color. The minimum number of colors necessary to complete a coloring for a particular graph is its chromatic number. The minimum number of colors necessary to complete a coloring for a particular graph is its chromatic number.

Two Color Problem The two color problem is as follows: Given a graph, determine whether or not two colors suffices to color it. The two color problem is as follows: Given a graph, determine whether or not two colors suffices to color it. There is a relatively quick solution to this problem. There is a relatively quick solution to this problem. However, no such solution for three colors exists. The three-color problem is NP- Complete, which, for now, is a way of saying that the problem is quite difficult. However, no such solution for three colors exists. The three-color problem is NP- Complete, which, for now, is a way of saying that the problem is quite difficult.

Two Color Algorithm Pick a start node arbitrarily and color it black. Pick a start node arbitrarily and color it black. Using, essentially a BFS, color all neighbors of this node white. Using, essentially a BFS, color all neighbors of this node white. Continue the BFS, coloring all neighbors of black nodes white and all neighbors of white nodes black. Continue the BFS, coloring all neighbors of black nodes white and all neighbors of white nodes black. If a conflict ever arises, (you need to color a node already assigned a color the opposite color), answer NO! If a conflict ever arises, (you need to color a node already assigned a color the opposite color), answer NO!

Two Color Alg. Cont. If all nodes are assigned a color, then clearly the answer is YES! If all nodes are assigned a color, then clearly the answer is YES! One trick: If the graph isn’t connected, you need to make sure and check all connected components. Thus, if the initial BFS doesn’t hit all nodes, then you must start another BFS from any uncolored node. Repeat until all nodes have been colored. One trick: If the graph isn’t connected, you need to make sure and check all connected components. Thus, if the initial BFS doesn’t hit all nodes, then you must start another BFS from any uncolored node. Repeat until all nodes have been colored.

Floyd’s All Shortest Paths This algorithm is known as “Floyd’s” and is based on dynamic programming. Basically, the idea of the algorithm is to construct a matrix that gives the length of the shortest path between each pair of nodes. This algorithm is known as “Floyd’s” and is based on dynamic programming. Basically, the idea of the algorithm is to construct a matrix that gives the length of the shortest path between each pair of nodes. We’ll initialize this matrix to our adjacency matrix and then perform N iterations. After each iteration k, the matrix will give the length of the shortest paths between each pair of nodes that use only nodes numbered 1 to k. We’ll initialize this matrix to our adjacency matrix and then perform N iterations. After each iteration k, the matrix will give the length of the shortest paths between each pair of nodes that use only nodes numbered 1 to k. At each iteration, the algorithm must check for each pair of nodes (i, j), whether or not there exists a path from i to j going through the node k that is better than the current shortest path using only nodes 1 to k-1. At each iteration, the algorithm must check for each pair of nodes (i, j), whether or not there exists a path from i to j going through the node k that is better than the current shortest path using only nodes 1 to k-1. After n iterations, the matrix will give the length of the shortest paths using any of the nodes, which is the result we want. After n iterations, the matrix will give the length of the shortest paths using any of the nodes, which is the result we want.

Floyd’s Algorithm adj:array[1..N, 1..N] of Boolean A:array[1..N, 1..N] of Boolean (A is our shortest path matrix) Floyd(){ A = adj; For k=1 to N { for i=1 to N { for j=1 to N A[i,j] = MIN(A[i,j], A[i,k]+A[k,j]) }}} Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k (compute weight of this edge) j k Uses only vertices numbered 1,…,k-1 i

Floyd’s Example Consider this graph with the adjacency matrix C Consider this graph with the adjacency matrix C Be careful with self-loops Be careful with self-loops Need to zero out the diagonals Need to zero out the diagonals But may be problem dependent! But may be problem dependent! So we’ll start with a modified adjacency matrix A 0 So we’ll start with a modified adjacency matrix A 0

Floyd’s Example Then we go through a loop where we update the shortest path from each node i to node j going through nodes 1 to k Then we go through a loop where we update the shortest path from each node i to node j going through nodes 1 to k So in step 1, we update each shortest path using only node 1 So in step 1, we update each shortest path using only node 1 A[i,j] = MIN(A[i,j], A[i,k]+A[k,j]) A[2,3] = MIN(A[2,3], A[2,1]+A[1,3]) A[2,3] = MIN(Inf, 3+5) = 8 All others remain unchanged this loop through All others remain unchanged this loop through

Floyd’s Example Loop k=2 Loop k=2 In step 2, we update each shortest path using only nodes 1 and 2 In step 2, we update each shortest path using only nodes 1 and 2 A[i,j] = MIN(A[i,j], A[i,k]+A[k,j]) A[3,1] = MIN(A[3,1], A[3,2]+A[2,1]) A[3,1] = MIN(Inf, 2+3) = 5 All others remain unchanged this loop through All others remain unchanged this loop through

Floyd’s Example Loop k=3 Loop k=3 In step 3, we update each shortest path using only nodes 1, 2 and 3 In step 3, we update each shortest path using only nodes 1, 2 and 3 A[i,j] = MIN(A[i,j], A[i,k]+A[k,j]) A[1,2] = MIN(A[1,2], A[1,3]+A[3,2]) A[3,1] = MIN(8, 5+2) = 7 All others remain unchanged this loop through All others remain unchanged this loop through

Minimum Spanning Tree A subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized A subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized Motivation Motivation Connecting islands with cable Connecting islands with cable Minimum sidewalks to connect buildings Minimum sidewalks to connect buildings Many more! Many more!

MST Algorithms Two algorithms Two algorithms Kruskal’s Kruskal’s Adds edges that won’t cause a cycle (stop after n-1 edges) Adds edges that won’t cause a cycle (stop after n-1 edges) Prim’s Prim’s Builds up a tree that includes all vertices (without cycles, obviously) Builds up a tree that includes all vertices (without cycles, obviously)

Kruskal’s Example Here is our original graph Here is our original graph We’ll begin by finding the smallest edge We’ll begin by finding the smallest edge Both AD and CE are the smallest (length 5) Both AD and CE are the smallest (length 5) We’ll arbitrarily choose AD We’ll arbitrarily choose AD

Kruskal’s Example Then we consider the smallest edges left and find the one that won’t create a cycle Then we consider the smallest edges left and find the one that won’t create a cycle CE is of length 5 and won’t create a cycle CE is of length 5 and won’t create a cycle Then, DF (of length 6) is chosen in a similar fashion Then, DF (of length 6) is chosen in a similar fashion

Kruskal’s Example The process continues… The process continues… Both AB and BE are of length 7 Both AB and BE are of length 7 We arbitrarily choose AB We arbitrarily choose AB Note that BD, if ever chosen, would create a cycle in our MST Note that BD, if ever chosen, would create a cycle in our MST

Kruskal’s Example We then select BE (length 7) as our next edge since it does not create a cycle We then select BE (length 7) as our next edge since it does not create a cycle Many more edges would now cause a cycle to be created if used later Many more edges would now cause a cycle to be created if used later BC, DE and EF BC, DE and EF

Kruskal’s Example Finally, we will consider both BC and EF (of length 8) but they will be rejected as they would create cycles Finally, we will consider both BC and EF (of length 8) but they will be rejected as they would create cycles So we consider BD and EG (of length 9) So we consider BD and EG (of length 9) BD would create a cycle BD would create a cycle EG will not so we choose it EG will not so we choose it

Kruskal’s Example We’ve chosen n-1 edges so we’ve created our minimum spanning tree! We’ve chosen n-1 edges so we’ve created our minimum spanning tree!

Summary Graphs are useful for representing lots of different problems Graphs are useful for representing lots of different problems A major topic area for the ICPC A major topic area for the ICPC A variety of algorithms A variety of algorithms We’ve covered the most important today but there are others We’ve covered the most important today but there are others Get to know these algorithms very well! Get to know these algorithms very well! Watch the run-time Watch the run-time

Thanks! Thanks for images and other information to: Thanks for images and other information to: Wikipedia Wikipedia Changhui (Charles) Yan Changhui (Charles) Yan