Graphs Graphs are collections of vertices and edges. Vertices are simple objects with associated names and other properties. Edges are connections between.

Slides:



Advertisements
Similar presentations
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
Advertisements

Review Binary Search Trees Operations on Binary Search Tree
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
PSU CS Algorithms Analysis and Design Graphs.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
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.
Graph & BFS.
Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices.
Testing for Connectedness and Cycles
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 9 Instructor: Paul Beame.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
Chapter 9: Graphs Basic Concepts
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Graph Operations And Representation. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Search Computing 2 COMP s1. P ROBLEMS ON G RAPHS What kinds of problems do we want to solve on/via graphs? Is there a simple path from A to.
Chapter 2 Graph Algorithms.
GRAPH THEORY.  A graph is a collection of vertices and edges.  An edge is a connection between two vertices (or nodes).  One can draw a graph by marking.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
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,
Graphs 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Graph Connectivity This discussion concerns connected components of a graph. Previously, we discussed depth-first search (DFS) as a means of determining.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 05 Introduction to Graph And Search Algorithms.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
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.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
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,
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
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.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Graph Operations And Representation
Chapter 9: Graphs Basic Concepts
Graph Operations And Representation
Graph Operations And Representation
Important Problem Types and Fundamental Data Structures
Chapter 9: Graphs Basic Concepts
For Friday Read chapter 9, sections 2-3 No homework
Presentation transcript:

Graphs Graphs are collections of vertices and edges. Vertices are simple objects with associated names and other properties. Edges are connections between two vertices. We can represent graphs pictorially by drawing points (vertices) and connecting them with lines (edges). However the graph is independent of the way it is represented. J L K M J L K M Two representations of the same graph

Paths A path is a list of vertices in which successive vertices are connected by edges in a graph. Example: BAFEG is a path. J L K M A BC G F D E H I

Connected Graphs A graph is connected if there is a path from every node to every other node in the graph 1. A graph that is not connected is made up of connected components. The graph below has three connected components. J L K M A BC G F D E H I 1 Intuitively, if the vertices were physical objects and the edges were strings connecting them, a connected graph would stay in one piece if picked up by any vertex.

Paths and Cycles A simple path is a path in which no vertex is repeated. Examples: BAFE is a simple path. BAFEGAC is not a simple path A cycle is a simple path except that the first and last vertex are the same (a path from a point back to itself). Example: AFEGA is a cycle. J L K M A BC G F D E H I

Trees and Forests A graph with no cycles is called a tree. A group of disconnected trees is called a forest. The figure below represents a forest of three disconnected trees. J L K M A BC G F D E H I

Spanning Trees A spanning tree of a graph is a subgraph that contains all the vertices but only enough of the edges to form a tree. The figure below shows a graph (left) and one of its possible spanning trees (right). A BC G F D E A BC G F D E

Spanning Trees If we add any edge to a spanning tree, it must form a cycle (since there is already a path between the two vertices that it connects). A tree with V vertices has exactly V-1 edges. If a tree has less than V-1 edges, it can't be connected. If it has more than V-1 edges, it must have a cycle. (But if it has exactly V-1 edges it need not be a tree!). A BC G F D E A BC G F D E

Sparse and Complete Graphs V: number of vertices E: number of edges E can range anywhere between 0 and 1/2(V)(V-1) Graphs with all edges present are called complete graphs. Graphs with relatively few edges (say less than VlogV) are called sparse. Graphs with relatively few of the possible edges missing are called dense. J L K M J L K M J L K M Complete Dense Sparse

Directed, Undirected, and Weighted Graphs So far we've dealt with undirected graphs, the simplest type. In weighted graphs, integers (weights) are assigned to each edge to represent, say, distances or costs. In directed graphs, edges are “one-way”: an edge may go from J to K but not from K to J. Directed weighted graphs are sometimes called networks. J L K M J L K M Directed Weighted Directed weighted (network) J L K M

Graph Representation We will look at two data structure representations of graphs. ● Adjacency matrix representation ● Adjacency structure representation Usually, the choice of representation depends on whether the graph in question is dense or sparse, however this also depends on the nature of the operations to be performed as well. The first step is to map the vertex names to integers. This is to allow accessing vertex information through an array index. Throughout, we will assume the existance of a function index which, when called with a vertex name, returns the integer representation of that vertex. We will also assume the existance of a function name which when called with a vertex index, returns the vertex name. In the examples to follow, we map vertices with single-character names to integers.

Adjacency Matrix Representation A BC G F D E This representation is suitable for dense graphs. A VxV array a of boolean values is maintained, with a[x,y] set to true if there is an edge from vertex x to vertex y and false otherwise. Each edge is represented by two bits: a[x,y] and a[y,x]. Obviously this is a symmetric matrix and even though we could store only half of it, it is inconvenient to do so from an indexing viewpoint. Note that it is usually convenient to assume that there's an “edge” from each vertex to itself, so a[x,x] is set to true. (In some cases, it is more convenient to set the diagonal elements to false.)

Adjacency Structure Representation This representation is suitable for sparse graphs. All vertices connected to each vertex are listed on an adjacency list for that vertex. This can be easily done with linked lists. A BC G F D E ABCDEEFG FAAFFDAA BEDFDE CGGE G Note that final outcome of adjacency lists depend on how the edges were input. For example, for adjacency list for ‘A’ above, the input edges were AG, AC, AB, AF.

Constructing Adjacency Lists #include using namespace std; const int maxV = 1000; typedef struct node { int v; node* next; } node, *nodePtr; nodePtr z = 0; int V=0, E=0; // vertex and edge count nodePtr adj[maxV]; // adjacency list int index(int i) { return i; }

Constructing Adjacency Lists void load() { int j, x, y; nodePtr t; int v1, v2; // can be other types. see 'index' cin >> V; // get number of vertices cin >> E; // get number of edges cout << V << " vertices, " << E << " edges." << endl; z = new(node); z->next = z; // init lists for (j=0; j<V; j++) adj[j] = z; // read edges for (j=0; j<E; j++) { cin >> v1; cin >> v2; // get edge vertices x = index(v1); y = index(v2); cout << "Edge " << j << ": " << x << ", " << y << endl; t = new node; t->v = x; t->next = adj[y]; adj[y] = t; t = new node; t->v = y; t->next = adj[x]; adj[x] = t; }

Constructing Adjacency Lists void list() { int i; nodePtr t; for (i=0; i<V; i++) { t = adj[i]; if (t != z) cout << i << " connected to "; while (t != z) { cout v << ((t->next == z) ? " " : ", "); t = t->next; } cout << endl; }

Depth-First Search When dealing with graphs, many questions can arise: Is the graph connected? If not, what are its connected components? Does the graph have a cycle? To answer these kinds of questions, depth-first search is used.

Depth-First Search void dfs() { int id=0, k; int val[maxV]; for (k=0; k<V; k++) val[k] = 0; for (k=0; k<V; k++) if (!val[k]) visit(val, k, id); }

Depth-First Search void visit(int* val, int k, int & id) { nodePtr t; cout << "visit: visiting node " << k << endl; val[k] = ++id; t = adj[k]; while (t != z) { if (val[t->v] == 0) visit(val, t->v, id); t = t->next; }

Depth-First Search DFS for a graph represented with adjacency lists requires time proportional to V + E. We set each of the V val values (hence the V term) and we examine each edge twice (hence the E term). DFS for a graph represented with an adjacency matrix require time proprotional to V 2 (every bit in the matrix is checked).

Non-Recursive Depth-First Search We can implement DFS non-recursively. We simply replace the frame stack with our own stack structure. Our dfs() function two slides back remains almost the same, however the function visit() is now made non-recursive. void dfs() { int id=0, k; int val[maxV]; // stack_init() // initialize the stack for (k=0; k<V; k++) val[k] = 0; for (k=0; k<V; k++) if (!val[k]) visit(val, k, id); }

Non-recursive Depth-First Search void visit(int* val, int k, int & id) { nodePtr t; cout << "visit: visiting node " << k << endl; stack_push(k); do { k = stack_pop(); val[k] = ++id; t = adj[k]; while (t != z) { if (val[t->v] == 0) { stack_push(t->v); val[t->v] = -1; } t = t->next; } } while (! Stackempty()); }

Breadth-First Search Rather than use a stack to hold vertices, as in DFS, here we use a queue. This leads to a second way of graph traversal, known as breadth-first search. To implement BFS, simply change stack operations to queue operations. Changing the data structure this way affects the order in which the nodes are visited.

DFS v. BFS In terms of vertex exploration, how do you think DFS and BFS differ? DFS: Explore deeper into the graph frontier (process closer vertices only after farther-away vertices have been explored). BFS: Sweep around starting point, exploring deeper only after all surrounding vertices in vicinity have been explored Order of traversal in either method is highly dependent on the order in which the vertices appear in the adjacency lists.