CS 101: Computer Programming and Utilization

Similar documents
CS 101: Computer Programming and Utilization

Computer Programming

CS 101: Computer Programming and Utilization

Lecture 7: General Loops (Chapter 7)

Computer Programming

Graphs. directed and undirected graphs weighted graphs adjacency matrices. abstract data type adjacency list adjacency matrix

CS 101: Computer Programming and Utilization

Computer Programming

1 Memory management in C++

CS 2113 Software Engineering

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

This lecture presents ordered lists. An ordered list is one which is maintained in some predefined order, such as alphabetical or numerical order.

Computer Programming

problem statement Prim s algorithm a multiple key map representation for a tree code for Prim s algorithm

Homework 6 Solutions CS330 Discrete Structures, Fall 2017

Computer Programming

Strongly Connected Components. Andreas Klappenecker

Midterm Exam. Sample Solutions

Graphs. Data Structures and Algorithms CSE 373 SU 18 BEN JONES 1

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

CS 101: Computer Programming

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11

1. Mother vertex using BFS. A mother vertex in a graph G = (V,E) is a vertex v such that all other vertices in G can be reached by a path from v.

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Midterm Practice Exam

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Computer Science II CSci 1200 Test 1 Overview and Practice

Computer Programming

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory

COMP 11 Class 17 Outline

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

CS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Quadratic Sorting Algorithms

Computer Programming

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

Datenstrukturen und Algorithmen

ENJOY! Problem 2 What does the following code do, in a brief English sentence? int mystery(int k) { int i = 0; if (k < 0) return -1;

Introduction to Programming

Come and join us at WebLyceum

Memory and Pointers written by Cathy Saxton

CS 101: Computer Programming and Utilization. Abhiram Ranade

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Unit 7. 'while' Loops

Linked List using a Sentinel

Memory, Arrays, and Parameters

Name SECTION: 12:45 2:20. True or False (12 Points)

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

PIC 10B Week 1 (Thurs)

Come and join us at WebLyceum

Computer Programming

Computer Programming

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Programming in C/C Lecture 2

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

Partha Sarathi Mandal

LAB 4.1 Relational Operators and the if Statement

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

CE221 Programming in C++ Part 1 Introduction

Templates and Vectors

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

If Control Construct

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Computer Programming

CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

CS242 COMPUTER PROGRAMMING

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

Topics. bool and string types input/output library functions comments memory allocation templates classes

And Even More and More C++ Fundamentals of Computer Science

CS 240 Data Structure Spring 2018 Exam I 03/01/2018

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

Functions! Objectives! 1E3! Topic 9! programming! n This topic should allow students to! n Read chapter 6 of the textbook now.!

Programming Language. Functions. Eng. Anis Nazer First Semester

Practice test for midterm 2

Project 2 - MapQuest Routing Problem

Data Structures CSci 1200 Test 1 Questions

CS 361 Data Structures & Algs Lecture 11. Prof. Tom Hayes University of New Mexico

CS 101: Computer Programming and Utilization

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

Transcription:

CS 101: Computer Programming and Utilization Jul-Nov 2017 Umesh Bellur (cs101@cse.iitb.ac.in) Lecture 17: Representing network of entities

About These Slides Based on Chapter 23 of the book An Introduction to Programming Through C++ by Abhiram Ranade (Tata McGraw Hill, 2014) Original slides by Abhiram Ranade First update by Sunita Sarawagi

Outline We would like to represent any object of interest on a computer Road map of India Electrical circuit MathemaCcal expressions... All of these are examples of graphs How to represent graphs on a computer

Graph Graph G = (V,E), where V = set of vercces E = set of edges = sets of pairs of vercces Example: Road map of India V = set of cices E = pairs of cices connected directly by a road Edges may be ordered or unordered Unordered: (u,v) and (v,u) both refer to the same edge Ordered: (u,v) and (v,u) refer to discnct edges Roadmap: edges are usually unordered However, we may choose ordered edges to indicate one- way roads. VerCces/Edges may be associated with atributes VerCces in road map may have names, e.g. city names Edges in road map may have names and lengths

A graph of friends VerCces = persons, Edge: connect friends Unordered: friendship is mutual Example: V ={Harry, Hermione, Ron, Draco, Crabbe E ={(Harry,Hermione), (Ron, Hermione), (Harry, Ron), (Draco, Crabbe) Ha He D R C

RepresenCng a person For every encty you should have a class What informacon would you put in each object of the class? struct Person{ ; string name; string address; vector<person*> friends; Person* or Person?

RepresenCng the 5 persons Person persons[5]; persons[0].name = Harry ; persons[1].name = Hermione ; persons[2].name = Ron ; persons[3].name = Draco ; persons[4].name = Crabbe ; Now we have created the vercces

Adding the edges Harry, Hermione are friends, so we should do... persons[0].friends.push_back(&persons[1]); persons[1].friends.push_back(&persons[0]); We need to make entries for both. So we could instead have a funccon void MF(Person &p, Person &q){ p.friends.push_back(&q); q.friends.push_back(&p); So now we just call it for each friendship: MF(persons[0],persons[1]); MF(persons[1],persons[2]); MF(persons[2],persons[0]); MF(persons[3],persons[4]);

Exercise Read in the name of a person and print that persons friends. cin >> name; for(int i=0; i<5; i++) if(name == persons[i].name){ for(size_t j=0; j<persons[i].friends.size(); j++) cout << persons[i].friends[j]-> name<<endl;

A C++11 Enhancement Read in the name of a person and print that persons friends. cin >> name; for(int i=0; i<5; i++) if(name == persons[i].name){ // fp is of type (Person * &) == auto for(auto fp : persons[i].friends) cout << fp->name << endl; Range based loop : for( type id : container){.. Block executed for all elements id of the container vectors, maps, are containers

Another enhancement: can we avoid the search completely? map<string,vector<string> > friends; friends["harry"].push_back("hermione"); friends["hermione"].push_back("harry");... // Print friends of all persons for(auto p : friends){ cout << p.first <<": "; for(auto f : p.second) cout << f <<' '; cout << endl;

What if edges have atributes? Suppose friendships have intensity SoluCon 1: struct Person{ string name; vector<person*> friends; vector<double> intensity; ;

SoluCon 2 struct EdgeData{ double intensity; double duration; ; struct Person{ string name; vector<person*> friends; vector<edgedata*> edgedata; ; void makefriends{person &p, Person &q, EdgeData *e){ p.friends.push_back(&q); p.edgedata.push_back(e); q.friends.push_back(&p); q.edgedata.push_back(e);

Remarks SoluCon 1 stores two copies of intensity in each of the two Person objects SoluCon 2 stores one copy; each Person object has a pointer to it. Will require less memory if there is a lot of edge data If there are mulcple copies of the same informacon we are always worried about updacng both copies consistently source of bugs. Vertex data and edge data can both be on the heap if needed.

Adjacency Matrix RepresentaCon struct VertexData{ string name;; struct EdgeData{ bool valid; double intensity, duration; ; VertexData v[nvertices]; EdgeData e[nvertices][nvertices]; If there is an edge from vertex i to vertex j, then set e[i][j].valid = true, e[i][j].intensity =... If no edge then set e[i][j].valid = false; Many variacons possible. See book.

Remarks For graph with V vercces and E edges Adjacency list uses: O(V+E) memory Adjacency matrix uses: O(V 2 ) memory Adjacency list is beter if graph has few edges.

Announcements Thursday graded lab. Cribs: empty, negacve marks for needless cribs. Help session.

Example Graph Queries Check if x and y are direct friends. map<string,vector<string> > friends; cin >> x >> y; bool xy_friends = false; for (string f : friends[x]) { if (f == y) { xy_friends = true; break;

Example Graph Queries Find friend of friends, or the set of nodes reachable by one hop on a graph. map<string,vector<string> > friends; cin >> query; map<string,int> friendsoffriends; for (string f : friends[query]) { for (string g : friends[f]) { friendoffriends[g]++; for (auto s : friendoffriends) cout << s.first << " ";

Example: Is there a path between two nodes? bool check_friends(string x, map<string,bool>& visited, string y) { if (x == y) return true; visited[x]=true; for (string f : friends[x]) { if (visited.find(f) == visited.end()) { if (check_friends(f, visited, y)) return true; return false; main() {cin >> x >> y; map<string,bool> visited; cout << check_friends(x,visited, y);

Graph between different types of nodes Web: Given pages, each with a url and a sequence of words in it. Given a query word, find all page- urls that contain it. View this as a graph with two types of nodes Page nodes Word nodes. Directed edges from pages to word that contain it.

Indexing the documents void loadpages(web &web) { map<string, vector<string> > pages; map<string, vector<string>> words; for (int i = 0; i < num_pages; i++) { string url; int num_words; cin >> url >> num_words; while (- - num_words) { string word; cin >> word; pages[url].push_back(word); words[word].push_back(url); while (true) { cin >> query; for (auto u : words[query]) { cout << u << " ";