Graphs. Graph Definitions A graph G is denoted by G = (V, E) where  V is the set of vertices or nodes of the graph  E is the set of edges or arcs connecting.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Problem solving with graph search
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
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.
 Graph Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Applied Discrete Mathematics Week 12: Trees
Graph & BFS.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graphs CS-240/341. Uses for Graphs computer networks and routing airline flights geographic maps course prerequisite structures tasks for completing a.
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.
Introduction to Graphs
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –Adjacency.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
Chapter 9: Graphs Basic Concepts
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
22C:19 Discrete Math Graphs Spring 2014 Sukumar Ghosh.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
GRAPH Learning Outcomes Students should be able to:
1  Introduction to Graph Data Structure  Applications  Graph Searching  Minimum Spanning Trees  Shortest Path problems.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Representing and Using Graphs
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 CS104 : Discrete Structures Chapter V Graph Theory.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
© Jalal Kawash 2010 Graphs Peeking into Computer Science.
© Jalal Kawash 2010 Graphs Peeking into Computer Science.
Shortest Path Problems Dijkstra’s Algorithm. Introduction Many problems can be modeled using graphs with weights assigned to their edges: Airline flight.
Data Structures & Algorithms Graphs
1 12/2/2015 MATH 224 – Discrete Mathematics Formally a graph is just a collection of unordered or ordered pairs, where for example, if {a,b} G if a, b.
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.
Graphs and MSTs Sections 1.4 and 9.1. Partial-Order Relations Everybody is not related to everybody. Examples? Direct road connections between locations.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
Chapter 9: Graphs.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
Graphs and Shortest Paths Using ADTs and generic programming.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
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.
Introduction to Graphs
Agenda Lecture Content: Introduction to Graph Path and Cycle
Introduction to Graphs
Graph Operations And Representation
Graphs Chapter 13.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Chapter 9: Graphs Basic Concepts
Graph Operations And Representation
Graphs.
Graphs G = (V, E) V are the vertices; E are the edges.
CSC 380: Design and Analysis of Algorithms
Graphs: Definitions How would you represent the following?
Chapter 9: Graphs Basic Concepts
Chapter 9 Graph algorithms
GRAPHS.
Introduction to Graphs
Introduction to Graphs
Presentation transcript:

Graphs

Graph Definitions A graph G is denoted by G = (V, E) where  V is the set of vertices or nodes of the graph  E is the set of edges or arcs connecting the vertices in V Each edge E is denoted as a pair (v,w) where v,w  V For example in the graph below V = {1, 2, 3, 4, 5, 6} E = {(1, 2) (2, 5) (3, 6) (4, 6) (5, 6)} This is an example of an unordered or undirected graph

Graph Definitions (contd.) If the pair of vertices is ordered then the graph is a directed graph or a di-graph Here, V = {1, 2, 3, 4, 5, 6} E = {(1, 2) (2, 5) (5, 6) (6, 3) (6, 4)} Vertex v is adjacent to w iff (v,w)  E Sometimes an edge has another component called a weight or cost. If the weight is absent it is assumed to be 1

Graph Definitions: Path A path is a sequence of vertices w 1, w 2, w 3,....w n such that (w i, w i+1 )  E Length of a path = # edges in the path A loop is an edge from a vertex onto itself. It is denoted by (v, v) A simple path is a path where no vertices are repeated along the path A cycle is a path with at least one edge such that the first and last vertices are the same, i.e. w 1 = w n

Graph Definitions: Connectedness A graph is said to be connected if there is a path from every vertex to every other vertex A connected graph is strongly connected if it is a connected graph as well as a directed graph A connected graph is weakly connected if it is  a directed graph that is not strongly connected, but,  the underlying undirected graph is connected Strong or Weak?

Applications of Graphs Driving Map  Edge = Road  Vertex = Intersection  Edge weight = Time required to cover the road Airline Traffic  Vertex = Cities serviced by the airline  Edge = Flight exists between two cities  Edge weight = Flight time or flight cost or both Computer networks  Vertex = Server nodes  Edge = Data link  Edge weight = Connection speed CAD/VLSI

Representing Graphs: Adjacency Matrix Adjacency Matrix  Two dimensional matrix of size n x n where n is the number of vertices in the graph  a[i, j] = 0 if there is no edge between vertices i and j  a[i, j] = 1 if there is an edge between vertices i and j  Undirected graphs have both a[i, j] and a[j, i] = 1 if there is an edge between vertices i and j  a[i, j] = weight for weighted graphs Space requirement is  (N 2 ) Problem: The array is very sparsely populated. For example if a directed graph has 4 vertices and 3 edges, the adjacency matrix has 16 cells only 3 of which are 1

Representing Graphs: Adjacency List Adjacency List  Array of lists  Each vertex has an array entry  A vertex w is inserted in the list for vertex v if there is an outgoing edge from v to w  Space requirement =  (E+V)  Sometimes, a hash-table of lists is used to implement the adjacency list when the vertices are identified by a name (string) instead of an integer

Adjacency List Example GraphAdjacency List An adjacency list for a weighted graph should contain two elements in the list nodes – one element for the vertex and the second element for the weight of that edge

Negative Cost Cycle A negative cost cycle is a cycle such that the sum of the costs of the edges is negative The more we cycle through a negative cost cycle, the lower the cost of the cycle becomes Cost of the path v 1 -v 5 No traversal of cycle: 6 One traversal of cycle: 0 Two traversals of cycle: -6 Three traversals of cycle: Negative cost cycles are not allowed when we traverse a graph to find the weighted shortest path

Using a model to solve a complicated traffic light problem Application of Graphs Using a model to solve a complicated traffic light problem GIVEN: A complex intersection. OBJECTIVE: Traffic light with minimum phases. SOLUTION: Identify permitted turns, going straight is a “turn”. Make group of permitted turns. Make the smallest possible number of groups. Assign each phase of the traffic light to a group.

Using a model to solve a complicated traffic light problem A B C D E An intersection

Using a model to solve a complicated traffic light problem Roads C and E are one way, others are two way. There are 13 permitted turns. Some turns such as AB (from A to B) and EC can be carried out simultaneously. Other like AD and EB cross each other and can not be carried out simultaneously. The traffic light should permit AB and EC simultaneously, but should not allow AD and EB.

Using a model to solve a complicated traffic light problem A B C D E An intersection AB & AC AD & EB

Using a model to solve a complicated traffic light problem SOLUTION: We model the problem using a structure called graph G(V,E). A graph consists of a set of points called vertices, and lines connecting the points, called edges. Drawing a graph such that the vertices represent turns. Edges between those turns that can NOT be performed simultaneously.

Using a model to solve a complicated traffic light problem A B C D E An intersection ABACAD BABCBD DADBDC EAEBECED Partial graph of incompatible turns

Partial table of incompatible turns Using a model to solve a complicated traffic light problem

SOLUTION: The graph can aid in solving our problem. A coloring of a graph is an assignment of a color to each vertex of the graph, so that no two vertices connected by an edge have the same color. Our problem is of coloring the graph of incompatible turns using as few colors as possible.

Using a model to solve a complicated traffic light problem More on SOLUTION (Graph coloring): The problem has been studied for decades. The theory of algorithms tells us a lot about it. Unfortunately this belongs to a class of problems called as NP-Complete problems. For such problems, all known solutions are basically “try all possibilities” In case of coloring, try all assignments of colors.

Using a model to solve a complicated traffic light problem Approaches to attempting NP-Complete problems: 1.If the problem is small, might attempt to find an optimal solution exhaustively. 2.Look for additional information about the problem. 3.Change the problem a little, and look for a good, but not necessarily optimal solution. An algorithm that quickly produces good but not necessarily optimal solutions is called a heuristic.

A reasonable heuristic for graph coloring is the greedy algorithm. Try to color as many vertices as possible with the first color, and then as many uncolored vertices with the second color, and so on. The approach would be: 1.Select some uncolored vertex, and color with new color. 2.Scan the list of uncolored vertices. For each uncolored vertex, determine whether it has an edge to any vertex already colored with the new color. If there is no such edge, color the present vertices with the new color.

This is called “greedy”, because it colors a vertex, whenever it can, without considering potential drawbacks