Graphs Definition of Graphs and Related Concepts

Slides:



Advertisements
Similar presentations
CS 1031 Graphs Definition of Graphs and Related Concepts Representation of Graphs The Graph Class Graph Traversal Graph Applications.
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
CS 1031 Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees Binary Trees Binary Search Trees.
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.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
1 Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees Binary Trees Binary Search Trees.
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
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs A New Data Structure
Graphs Definition of Graphs and Related Concepts
Elementary Graph Algorithms
CS212: Data Structures and Algorithms
Thinking about Algorithms Abstractly
A vertex u is reachable from vertex v iff there is a path from v to u.
Data Structures Graphs - Terminology
Graph Traversals Some algorithms require that every vertex of a graph be visited exactly once. The order in which the vertices are visited may be important,
Definition of Graphs A graph is a finite set of nodes with edges between nodes Formally, a graph G is a structure (V,E) consisting of a finite set V called.
Definition of Graphs A graph is a finite set of nodes with edges between nodes Formally, a graph G is a structure (V,E) consisting of a finite set V called.
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Introduction to Graphs
Ellen Walker CPSC 201 Data Structures Hiram College
CC 215 Data Structures Graph Searching
Depth-First Search.
CS202 - Fundamental Structures of Computer Science II
Graph Algorithms Using Depth First Search
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph.
Graph Representation, DFS and BFS
CSE 421: Introduction to Algorithms
Graph & BFS.
Graphs Chapter 13.
Search Related Algorithms
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
Connected Components, Directed Graphs, Topological Sort
Graphs Part 2 Adjacency Matrix
CSE 373 Data Structures Lecture 16
Unit - 3 Trees - Binary tress - Terminology - Representation
COMP108 Algorithmic Foundations Graph Theory
Graphs Chapter 7 Visit for more Learning Resources.
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
A vertex u is reachable from vertex v iff there is a path from v to u.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graphs.
Graphs.
Graphs.
Important Problem Types and Fundamental Data Structures
3.2 Graph Traversal.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees
Graphs.
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 Definition of Graphs and Related Concepts Representation of Graphs The Graph Class Graph Traversal Graph Applications CS 103

Definition of Graphs A graph is a finite set of nodes with edges between nodes Formally, a graph G is a structure (V,E) consisting of a finite set V called the set of nodes, and a set E that is a subset of VxV. That is, E is a set of pairs of the form (x,y) where x and y are nodes in V CS 103

Examples of Graphs V={0,1,2,3,4} When (x,y) is an edge, we say that x is adjacent to y, and y is adjacent from x. 0 is adjacent to 1. 1 is not adjacent to 0. 2 is adjacent from 1. CS 103 2 4 3

A “Real-life” Example of a Graph V=set of 6 people: John, Mary, Joe, Helen, Tom, and Paul, of ages 12, 15, 12, 15, 13, and 13, respectively. E ={(x,y) | if x is younger than y} Mary Helen CS 103 Joe John Tom Paul

Intuition Behind Graphs The nodes represent entities (such as people, cities, computers, words, etc.) Edges (x,y) represent relationships between entities x and y, such as: “x loves y” “x hates y” “x is a friend of y” (note that this not necessarily reciprocal) “x considers y a friend” “x is a child of y” “x is a half-sibling of y” “x is a full-sibling of y” In those examples, each relationship is a different graph CS 103

Graph Representation For graphs to be computationally useful, they have to be conveniently represented in programs There are two computer representations of graphs: Adjacency matrix representation Adjacency lists representation CS 103

Adjacency Matrix Representation In this representation, each graph of n nodes is represented by an n x n matrix A, that is, a two- dimensional array A The nodes are (re)-labeled 1,2,…,n A[i][j] = 1 if (i,j) is an edge A[i][j] = 0 if (i,j) is not an edge CS 103

Example of Adjacency Matrix 1 0 1 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 4 0 A = 2 4 CS 103 3

Another Example of Adj. Matrix Re-label the nodes with numerical labels Mary 0 Helen 1 0 0 0 0 0 0 1 1 0 0 1 1 1 1 0 0 0 0 A = Joe 3 John 2 CS 103 Tom 4 Paul 5

Pros and Cons of Adjacency Matrices Simple to implement Easy and fast to tell if a pair (i,j) is an edge: simply check if A[i][j] is 1 or 0 Cons: No matter how few edges the graph has, the matrix takes O(n2) in memory CS 103

Adjacency Lists Representation A graph of n nodes is represented by a one- dimensional array L of linked lists, where L[i] is the linked list containing all the nodes adjacent from node i. The nodes in the list L[i] are in no particular order CS 103

Example of Linked Representation L[0]: empty L[1]: empty L[2]: 0, 1, 4, 5 L[3]: 0, 1, 4, 5 L[4]: 0, 1 L[5]: 0, 1 Helen 1 Mary 0 Joe 3 John 2 Tom 4 Paul 5 CS 103

Pros and Cons of Adjacency Lists Saves on space (memory): the representation takes as many memory words as there are nodes and edge. Cons: It can take up to O(n) time to determine if a pair of nodes (i,j) is an edge: one would have to search the linked list L[i], which takes time proportional to the length of L[i]. CS 103

The Graph Class class Graph { public: typedef int datatype; typedef datatype * datatypeptr; Graph( int n=0); // creates a graph of n nodes and no edges bool isEdge( int i, int j); void setEdge( int i, int j, datatype x); int getNumberOfNodes(){return numberOfNodes;}; private: datatypeptr *p; //a 2-D array, i.e., an adjacency matrix int numberOfNodes; }; CS 103

Graph Class Implementation Graph::Graph( int n){ assert(n>=0); numberOfNodes=n; if (n==0) p=NULL; else{ p = new datatypeptr[n]; for (int i=0;i<n;i++){ p[i] = new datatype[n]; for (int j=0;j<n;j++) p[i][j]=0; } }; bool Graph::isEdge(int i, int j){ assert(i>=0 && j>=0); return p[i][j] != 0; }; void Graph:;setEdge(int i, int j, datatype x){ assert(i>=0 && j>=0); p[i][j]=x; }; CS 103

Directed vs. Undirected Graphs If the directions of the edges matter, then we show the edge directions, and the graph is called a directed graph (or a digraph) The previous two examples are digraphs If the relationships represented by the edges are symmetric (such as (x,y) is edge if and only if x is a sibling of y), then we don’t show the directions of the edges, and the graph is called an undirected graph. CS 103

Examples of Undirected Graphs V=set of 6 people: John, Mary, Joe, Helen, Tom, and Paul, where the first 4 are siblings, and the last two are siblings E ={(x,y) | x and y are siblings} if (x,y) is an edge: we say that x is adjacent to y, & y adjacent to x. We also say that x and y are neighbors Mary Helen CS 103 Joe John Tom Paul

Representations of Undirected Graphs The same two representations for directed graphs can be used for undirected graphs Adjacency matrix A: A[i][j]=1 if (i,j) is an edge; 0 otherwise Adjacency Lists: L[i] is the linked list containing all the neighbors of i CS 103

Example of Representations Mary 0 Helen 1 Linked Lists: L[0]: 1, 2, 3 L[1]: 0, 2, 3 L[2]: 0, 1, 3 L[3]: 0, 1, 2 L[4]: 5 L[5]: 4 John 2 Joe 3 Tom 4 Paul 5 Adjacency Matrix: 0 1 1 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 A = CS 103

Definition of Some Graph Related Concepts Let G be a directed graph The indegree of a node x in G is the number of edges coming to x The outdegree of x is the number of edges leaving x. Let G be an undirected graph The degree of a node x is the number of edges that have x as one of their end nodes The neighbors of x are the nodes adjacent to x CS 103

Things for You To Do Add a member function to the class graph, called getIndegree( int x), which returns the indegree of node x Add a member function to the class graph, called getOutdegree( int x), which returns the outdegree of node x CS 103

Paths A path in a graph G is a sequence of nodes x1, x2, …,xk, such that there is an edge from each node the next one in the sequence For example, in the first example graph, the sequence 3, 0, 1, 2 is a path, but the sequence 0, 3, 4 is not a path because (0,3) is not an edge In the “sibling-of” graph, the sequence John, Mary, Joe, Helen is a path, but the sequence Helen, Tom, Paul is not a path CS 103

Graph Connectivity An undirected graph is said to be connected if there is a path between every pair of nodes. Otherwise, the graph is disconnected Informally, an undirected graph is connected if it hangs in one piece CS 103 Connected Disconnected

Connected Components If an undirected graph is not connected, then each “piece” is called a connected component. A piece in itself is connected, but if you bring any other node to it from the graph, it is no longer connected If the graph is connected, then the whole graph is one single connected component Of Interest: Given any undirected graph G, Is G connected? If not, find its connected components. CS 103

Graph Traversal Techniques The previous connectivity problem, as well as many other graph problems, can be solved using graph traversal techniques There are two standard graph traversal techniques: Depth-First Search (DFS) Breadth-First Search (BFS) CS 103

Graph Traversal (Contd.) In both DFS and BFS, the nodes of the undirected graph are visited in a systematic manner so that every node is visited exactly one. Both BFS and DFS give rise to a tree: When a node x is visited, it is labeled as visited, and it is added to the tree If the traversal got to node x from node y, y is viewed as the parent of x, and x a child of y CS 103

Depth-First Search DFS follows the following rules: Select an unvisited node x, visit it, and treat as the current node Find an unvisited neighbor of the current node, visit it, and make it the new current node; If the current node has no unvisited neighbors, backtrack to the its parent, and make that parent the new current node; Repeat steps 3 and 4 until no more nodes can be visited. If there are still unvisited nodes, repeat from step 1. CS 103

Illustration of DFS 1 2 1 4 9 4 5 7 10 2 11 9 8 5 6 7 11 Graph G 6 8 1 2 1 4 9 4 5 7 10 2 11 9 8 5 6 CS 103 7 11 Graph G 6 8 10 DFS Tree

Implementation of DFS Observations: the last node visited is the first node from which to proceed. Also, the backtracking proceeds on the basis of "last visited, first to backtrack too". This suggests that a stack is the proper data structure to remember the current node and how to backtrack. CS 103

Illustrate DFS with a Stack We will redo the DFS on the previous graph, but this time with stacks In Class CS 103

DFS (Pseudo Code) DFS(input: Graph G) { Stack S; Integer x, t; while (G has an unvisited node x){ visit(x); push(x,S); while (S is not empty){ t := peek(S); if (t has an unvisited neighbor y){ visit(y); push(y,S); } else pop(S); } CS 103

C++ Code for DFS int * dfs(Graph G){ // returns a parent array representing the DFS tree int n=G.getNumberOfNodes(); int * parent = new int[n]; Stack S(n); bool visited[n]; for ( int i=0; i<n; i++) visited[i]=false; int x=0; // begin DFS from node 0 int numOfConnectedComponents=0; while (x<n){ // begin a new DFS from x numOfConnectedComponents++; visited[x]=true; S.push(x); parent[x] = -1; // x is root while(!S.isEmpty()) // traverse the current piece // insert here the yellow box from the next slide x= getNextUnvisited(visited,n,x); } cout<<“Graph has “<< numOfConnectedComponents<< “ connected components\n”; return p; CS 103

int y=getNextUnvisitedNeighbor( t,G,visited,n); if (y<n){ int t=S.peek( ); int y=getNextUnvisitedNeighbor( t,G,visited,n); if (y<n){ visited[y]=true; S.push(y); parent[y]=t; } else S.pop( ); // Put this before dfs(…) // returns the leftmost unvisited // neighbor of node t. If none // remains, returns n. int getNextUnvisitedNeighbor(int t, graph G, bool visited[],int n){ for (int j=0;j<n;j++) if (G.isEdge(t,j) && !visited[j]) return j; // if no unvisited neighbors left: return n; } CS 103 //Put this before dfs(…). This returns the next unvisited node, or n otherwise int getNextUnvisited(bool visited[],int n, int lastVisited){ int j=lastVisited+1; while (visited[j] && j<n) j++; return j; }

Breadth-First Search BFS follows the following rules: Select an unvisited node x, visit it, have it be the root in a BFS tree being formed. Its level is called the current level. From each node z in the current level, in the order in which the level nodes were visited, visit all the unvisited neighbors of z. The newly visited nodes from this level form a new level that becomes the next current level. Repeat step 2 until no more nodes can be visited. If there are still unvisited nodes, repeat from Step 1. CS 103

Illustration of BFS 1 2 2 4 1 4 9 5 9 10 5 7 10 11 8 6 7 8 11 6 CS 103 Graph G BFS Tree

Implementation of DFS Observations: the first node visited in each level is the first node from which to proceed to visit new nodes. This suggests that a queue is the proper data structure to remember the order of the steps. CS 103

Illustrate BFS with a Queue We will redo the BFS on the previous graph, but this time with queues In Class CS 103

BFS (Pseudo Code) BFS(input: graph G) { Queue Q; Integer x, z, y; while (G has an unvisited node x) { visit(x); Enqueue(x,Q); while (Q is not empty){ z := Dequeue(Q); for all (unvisited neighbor y of z){ visit(y); Enqueue(y,Q); } CS 103

Things for you to Do Give a C++ implementation of BFS, using the pseudo code as your guide Use BFS as a way to determine of the input graph G is connected, and if not, to output the number of connected components Modify BFS so that the level of each node in the BFS tree is computed. Give another class for graph, this time using a linked lists representation. CS 103

Graph Traversals (Search) We have covered some of these with binary trees Breadth-first search (BFS) Depth-first search (DFS) A traversal (search): An algorithm for systematically exploring a graph Visiting (all) vertices Until finding a goal vertex or until no more vertices Only for connected graphs

Breadth-first search One of the simplest algorithms Also one of the most important It forms the basis for MANY graph algorithms

BFS: Level-by-level traversal Given a starting vertex s Visit all vertices at increasing distance from s Visit all vertices at distance k from s Then visit all vertices at distance k+1 from s Then ….

BFS in a binary tree (reminder) BFS: visit all siblings before their descendents 5 2 8 3 6 1 10 7 9 5 2 8 1 3 6 10 7 9

This version for binary trees only! BFS(tree t) q  new queue enqueue(q, t) while (not empty(q)) curr  dequeue(q) visit curr // e.g., print curr.datum enqueue(q, curr.left) enqueue(q, curr.right) This version for binary trees only!

BFS for general graphs This version assumes vertices have two children left, right This is trivial to fix But still no good for general graphs It does not handle cycles Example.

Queue: A A B E C D G F Start with A. Put in the queue (marked red)

Queue: A B E A B E C D G F B and E are next

Queue: A B E C G D F A B E C D G F When we go to B, we put G and C in the queue When we go to E, we put D and F in the queue

Queue: A B E C G D F A B E C D G F When we go to B, we put G and C in the queue When we go to E, we put D and F in the queue

Queue: A B E C G D F F A B E C D G F Suppose we now want to expand C. We put F in the queue again!

Generalizing BFS Cycles: We need to save auxiliary information Each node needs to be marked Visited: No need to be put on queue Not visited: Put on queue when found What about assuming only two children vertices? Need to put all adjacent vertices in queue

BFS(graph g, vertex s) unmark all vertices in G q  new queue mark s enqueue(q, s) while (not empty(q)) curr  dequeue(q) visit curr // e.g., print its data for each edge <curr, V> if V is unmarked mark V enqueue(q, V)

The general BFS algorithm Each vertex can be in one of three states: Unmarked and not on queue Marked and on queue Marked and off queue The algorithm moves vertices between these states

Handling vertices Unmarked and not on queue: Marked and on queue: Not reached yet Marked and on queue: Known, but adjacent vertices not visited yet (possibly) Marked and off queue: Known, all adjacent vertices on queue or done with

Queue: A A B E C D G F Start with A. Mark it.

Queue: A B E A B E C D G F Expand A’s adjacent vertices. Mark them and put them in queue.

Queue: A B E C G A B E C D G F Now take B off queue, and queue its neighbors.

Queue: A B E C G D F A B E C D G F Do same with E.

Queue: A B E C G D F A B E C D G F Visit C. Its neighbor F is already marked, so not queued.

Queue: A B E C G D F A B E C D G F Visit G.

Queue: A B E C G D F A B E C D G F Visit D. F, E marked so not queued.

Queue: A B E C G D F A B E C D G F Visit F. E, D, C marked, so not queued again.

Queue: A B E C G D F A B E C D G F Done. We have explored the graph in order: A B E C G D F.

Interesting features of BFS Complexity: O(|V| + |E|) All vertices put on queue exactly once For each vertex on queue, we expand its edges In other words, we traverse all edges once BFS finds shortest path from s to each vertex Shortest in terms of number of edges Why does this work?

Depth-first search Again, a simple and powerful algorithm Given a starting vertex s Pick an adjacent vertex, visit it. Then visit one of its adjacent vertices ….. Until impossible, then backtrack, visit another

DFS(graph g, vertex s) Assume all vertices initially unmarked mark s visit s // e.g., print its data for each edge <s, V> if V is not marked DFS(G, V)

Current vertex: A A B E C D G F Start with A. Mark it.

Current: B A B E C D G F Expand A’s adjacent vertices. Pick one (B). Mark it and re-visit.

Current: C A B E C D G F Now expand B, and visit its neighbor, C.

Current: F A B E C D G F Visit F. Pick one of its neighbors, E.

Current: E A B E C D G F E’s adjacent vertices are A, D and F. A and F are marked, so pick D.

Current: D A B E C D G F Visit D. No new vertices available. Backtrack to E. Backtrack to F. Backtrack to C. Backtrack to B

Current: G A B E C D G F Visit G. No new vertices from here. Backtrack to B. Backtrack to A. E already marked so no new.

Current: 1 A 2 5 B E 6 3 C D G 7 4 F Done. We have explored the graph in order: A B C F E D G

Interesting features of DFS Complexity: O(|V| + |E|) All vertices visited once, then marked For each vertex on queue, we examine all edges In other words, we traverse all edges once DFS does not necessarily find shortest path Why?