Graph C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
C and Data Structures Baojian Hua
Advertisements

Lecture 15. Graph Algorithms
CSE Lectures 18 – Graphs Graphs & Characteristics
Data Structure & Abstract Data Type
C Module System C and Data Structures Baojian Hua
Elementary Data Structures: Part 2: Strings, 2D Arrays, Graphs
Extensible Array C and Data Structures Baojian Hua
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Queue C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Relation Discrete Mathematics and Its Applications Baojian Hua
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Breath First Searching & Depth First Searching C and Data Structures Baojian Hua
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
String C and Data Structures Baojian Hua
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Stack C and Data Structures Baojian Hua
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
C and Data Structures Baojian Hua
Graph C and Data Structures Baojian Hua
Hash Discrete Mathematics and Its Applications Baojian Hua
Queue C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Linked List C and Data Structures Baojian Hua
Tree C and Data Structures Baojian Hua
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
String C and Data Structures Baojian Hua
Graph Traversal Discrete Mathematics and Its Applications Baojian Hua
Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Graph Discrete Mathematics and Its Applications Baojian Hua
Hash C and Data Structure Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
Graph Traversal C and Data Structures Baojian Hua
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Computer Science: A Structured Programming Approach Using C Graphs A graph is a collection of nodes, called vertices, and a collection of segments,
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Hash C and Data Structure Baojian Hua
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 Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs Upon completion you will be able to:
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
Extensible Tree Discrete Mathematics and Its Applications Baojian Hua
Design and Analysis of Algorithms Introduction to graphs, representations of a graph Haidong Xue Summer 2012, at GSU.
Graphs and Shortest Paths Using ADTs and generic programming.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/
CS120 Graphs.
CS200: Algorithm Analysis
Discrete Mathematics and
Graph Theory By Amy C. and John M..
Discrete Mathematics and
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Presentation transcript:

Graph C and Data Structures Baojian Hua

What ’ s a Graph? Graph: a group of vertices connected by edges

Why Study Graphs? Interesting & broadly used abstraction not only in computer science challenge branch in discrete math Ex: the 4-color problem hundreds of known algorithms with more to study numerous applications

Broad Applications GraphVerticesEdges communicationtelephonecables softwarefunctionscalls internetweb pagehyper-links social relationship peoplefriendship transportationcitiesroads ………

Graph Terminology node edge: directed vs undirected

Graph Terminology degree: in-degree vs out-degree

Graph ADT A graph is a tuple (V, E) where V is a set of vertices v1, v2, … and E is a set of vertex tuple Typical operations: graph creation search a vertex (or an edge) traverse all graph vertexes …

Example G = (V, E) V = {1, 2, 3, 4, 5, 6} E = {(1, 2), (2, 5), (5, 4), (4, 1), (4, 2), (3, 5), (3, 6), (6, 6)}

“ graph ” ADT in C: Interface // We assume, in this slides, all graphs directed, // and undirected ones are similar and easier. // in file “graph.h” #ifndef GRAPH_H #define GRAPH_H typedef struct graph *graph; graph new (); void insertVertex (graph g, poly x); void insertEdge (graph g, poly from, poly to); // we’d see more later … #endif

Client Code graph g = newGraph (); insertVertex (g, 1); insertVertex (g, 2); … insertEdge (g, 1, 2); insertEdge (g, 2, 5); …

Implementation We ’ d study two impl ’ strategies: array-based for vertex v0, v1, …, vn, keep a two dimension array G[n+1][n+1] G[i][j] holds the edge, if there exists one linear list-based for every vertex vi, maintain a linear list l l stores vi ’ s adjacent vertices

Implementation#1: Adjacency Matrices // A simplified version (no edge info): #define initSize 4 struct graph { int (*matrix)[initSize][initSize]; int maxSize; int curSize; }; matrix maxSize g curSize

graph new () { graph g = checkedMalloc (sizeof (*g)); g->matrix = checkedMalloc (sizeof (g->matrix)); for (int i=0; i<initSize; i++) for (int j=0; j<initSize; j++) (g->matrix)[i][j] = 0; g->maxSize = initSize; g->curSize = -1; return g; }

new () matrix maxSize g curSize=-1

insertVertex (graph g, poly x) void insertVertex (graph g, poly x) { // assign each vertex x a number (array index) // maintain a dictionary internally int index = assignIndex (x); if (index >= g->maxSize) { // extend the array, just like the extArray extendArray (g->matrix); g->maxSize = …; //depends on extension factor } g->curSize ++; return; }

insertVertex on This Graph graph g = newGraph (); insertVertex (g, 1); insertVertex (g, 2); … // suppose we have this mapping: 1==>0; 2==>1; …

insertVertex matrix maxSize g curSize=4 After inserting vertex 1, 2, 3, 4; the matrix is full, so we ’ d extend it. Suppose the extension factor is 2.

insertVertex matrix maxSize g curSize=

insertEdge (graph g, poly from, poly to) void insertEdge (graph g, poly from, poly to) { // assign vertex from, to numbers (array index) // maintain internal a mapping int indexFrom = assignIndex (from); int indexTo = assignIndex (to); (g->matrix)[indexFrom][indexTo] = 1; return; }

insertVertex on This Graph graph g = newGraph (); insertEdge (g, 1, 2); insertEdge (g, 2, 5); …

insertVertex matrix maxSize g curSize=

Moral We should keep a dictionary (symbol table) to record and assign array index to graph vertex any representation strategy should do the dictionary should be kept in the graph Information in the matrix is too coarse we may want to associate some info (say weight) with graph edges Need a more fancy version of array slots

More Fancy Edge Definition // define a more fancy edge: typedef struct edge *edge; struct edge { int from; int to; poly info; };

Adjacency Matrices // A simplified version: #define initSize 4 struct graph { edge (*matrix)[initSize][initSize]; int maxSize; int curSize; }; matrix maxSize g curSize

insertEdge (graph g, poly from, poly to) void insertEdge (graph g, poly from, poly to, poly edgeInfo) { // assign vertex from, to numbers (array index) int indexFrom = assign (from); int indexTo = assign (to); edge e = newEdge (indexFrom, indexTo, edgeInfo); (g->matrix)[indexFrom][indexTo] = e; return; }

Implementation#2: Adjacency Lists // in file “alGraph.c” struct graph{ poly info; struct succ *succs; struct graph *next; }; struct succ{ struct graph *v; struct succ *next; }; info succs next info succs next v v v /\

Or With Edges struct graph{ poly info; struct edge *edges; struct graph *next; }; struct edge { struct vertex *from; struct vertex *to; struct edge *next; }; info edges next from next to /\

Use of Linear List // syntax T is not supported by C, which is // put here for illustrative purpose struct graph{ list vertices; }; struct vertex{ poly info; list edges; }; struct edge{ struct vertex *from; struct vertex *to; } info edges next from next to /\

graph new () { graph g = checkedMalloc (sizeof (*g)); g->vertices = newLinkedList (); return g; } /\ g

insertVertex (graph g, poly x) void insertVertex (graph g, poly x) { vertex v = newVertex (x); linkedListInsertHead (g->vertices, v); return; } // with void newVertex (poly x){ vertex v = checkedMalloc (sizeof (*v)); v->info = x; v->edges = newLinkedList (); return v; }

insertVertex on This Graph graph g = newGraph (); insertVertex (g, 1); insertVertex (g, 2); …

Example of InsertVertex next data g /\

insertEdge (graph g, poly from, poly to) void insertEdge (graph g, poly from, poly to) { vertex f = searchVertex (g->vertices, from); vertex t = searchVertex (g->vertices, to); edge e = newEdge (f, t); linkedListInsertHead (f->edges, e); return; }

insertVertex on This Graph graph g = newGraph (); insertEdge (g, 1, 2); insertVertex (g, 2, 5); …

Example of InsertEdge next data g /\ from to from to